Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js sort by array key value

myArray.sort((a, b) => a.key - b.key)
Comment

array sort by key javascript

function sortByKey(array, key) {
  return array.sort((a, b) => {
    let x = a[key];
    let y = b[key];
    
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  });
}
Comment

sort from the key value js

var masterList = [
    { key: 1, val: "google" },
    { key: 2, val: "yahoo" },
    { key: 3, val: "msn" },
    { key: 4, val: "stackoverflow" },
    { key: 5, val: "github" },
    { key: 6, val: "jsfiddle" },
    { key: 7, val: "amazon" },
    { key: 8, val: "ebay" }
];

masterList.sort((a,b)=> (a.val < b.val ? 1 : -1)) //For Decending

masterList.sort((a,b)=> (a.val > b.val ? 1 : -1)) //For Ascending

console.log( masterList );
Comment

PREVIOUS NEXT
Code Example
Javascript :: ajax datatable reload paging retained 
Javascript :: remove appended element jquery 
Javascript :: react router next page top 
Javascript :: hide and show modal in jquery 
Javascript :: js api call 
Javascript :: UpperCase every first letter in each word in str 
Javascript :: how to check if a string is in a string js 
Javascript :: js window active 
Javascript :: comment in react 
Javascript :: go to top angular 
Javascript :: split text and join in js 
Javascript :: react native elevation 
Javascript :: on spacebar press javascript 
Javascript :: google script wait 
Javascript :: Deleting all white spaces in a string 
Javascript :: react materilize 
Javascript :: neo4j create relationship between existing nodes 
Javascript :: how to find closest img tag in jquery 
Javascript :: javascript pdf preview 
Javascript :: jquery replace h1 with h2 
Javascript :: copy file javascript 
Javascript :: bootstrap js, jQuery, popper cdn 
Javascript :: js maximum number value 
Javascript :: extract words from string js 
Javascript :: bignumber to number javascript 
Javascript :: mongoose limit 
Javascript :: react navigation navigator types 
Javascript :: get screen resolution jquery 
Javascript :: Binary Agents 
Javascript :: how to sort an array of objects by a property value in javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =