Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

array sort by two properties

console.log(fruits.sort((a, b)=> {
  if (a.colA === b.colA){
    return a.colB < b.colB ? -1 : 1
  } else {
    return a.colA < b.colA ? -1 : 1
  }
}))

/*
> [{colA: "apple", colB: "granny smith"},
  {colA: "apple", colB: "red delicious"},
  {colA: "orange", colB: "blood"},
  {colA: "orange", colB: "clementine"},
  {colA: "orange", colB: "navel"},
  {colA": "pear", colB: "green anjou"},
  {colA: "pear", colB: "red anjou"}]
  */
Source by levelup.gitconnected.com #
 
PREVIOUS NEXT
Tagged: #array #sort #properties
ADD COMMENT
Topic
Name
2+7 =