Answers for "arrays.sort"

25

JS array sort

numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
Posted by: Guest on May-08-2021
8

sort array java

Here’s the java program to sort an array using Arrays.sort() method.

import java.util.Arrays;
public class JavaArraySortMethod
{
   public static void main(String[] args)
   {
      String[] strGiven = {"Great Barrier Reef", "Paris", "borabora", "Florence","tokyo", "Cusco"};
      Arrays.sort(strGiven);
      System.out.println("Output(case sensitive) : " + Arrays.toString(strGiven));
   }
}
Posted by: Guest on October-23-2020
1

js sort

strData = ["banana", "apple", "zebra", "jos"];
  numData = [1, 3, 2, 5, 6];
  objData = [
    { name: 'Alex', age: 20 },
    { name: 'Karlsson', age: 19 },
    { name: 'Noah', age: 21 },
  ];


  // sort str alfabeticly (a-z)
  strData.sort((a,b) => {
    return a === b ? 0 : a < b ? -1 : 1
  });
  console.log(strData)


  // sort Objdata uppifrån å ner (ålder 19-21)
  objData.sort((a,b) => a.age - b.age)
  console.log(objData);


  // Small number to large number
  numData.sort()
  console.log(numData);
  // Large number to small number
  numData.sort((a,b) => b-a)
  console.log(numData);
Posted by: Guest on October-11-2021
3

sort js

numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
Posted by: Guest on October-20-2020
13

javascript sort

var names = ["Peter", "Emma", "Jack", "Mia", "Eric"];
names.sort(); // ["Emma", "Eric", "Jack", "Mia", "Peter"]

var objs = [
  {name: "Peter", age: 35},
  {name: "Emma", age: 21},
  {name: "Jack", age: 53}
];

objs.sort(function(a, b) {
  return a.age - b.age;
}); // Sort by age (lowest first)
Posted by: Guest on May-21-2020
1

arr.sort()

const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b}); // for ascending
//another way for ascending
points.sort((a,b)=>{
  if(a>b){
      return 1
  }else if(a<b){
      return -1
  }else{
      return 0
  }
})
points.sort(function(a, b){return b - a}); // for descending
//another way for descending
points.sort((a,b)=>{
  if(a>b){
      return -1
  }else if(a<b){
      return 1
  }else{
      return 0
  }
})
Posted by: Guest on August-23-2021

Browse Popular Code Answers by Language