3

I have an an array with key-value pair, array columns are id and name. I want to sort this array by id.

The id column value is of type string type but I want to sort them as numeric values.

var items = [
{
    "id": "165",
    "name": "a"
},
{
    "id": "236",
    "name": "c"
},
{
    "id": "376",
    "name": "b"
},
{
    "id": "253",
    "name": "f"
},
{
    "id": "235",
    "name": "e"
},
{
    "id": "24",
    "name": "d"
},
{
    "id": "26",
    "name": "d"
}
]
3
  • Okay, thanks for the explanation. What is your actual question? Commented Jun 28, 2018 at 10:20
  • Not Possible duplicate value according to my code BraveButter
    – Anshul
    Commented Jun 28, 2018 at 10:21
  • My question is I want to sort this array by Id column but when i sort it on server side code it shows 1,10,100,11,111,12,2 which is wrong i want 1,2,34.... Krishna Prashatt
    – Anshul
    Commented Jun 28, 2018 at 10:23

5 Answers 5

4
items.sort((a,b)=>a.id-b.id)
1
  • This won't work on older browsers, but is widely supported.
    – Hudson
    Commented Jun 28, 2018 at 10:21
1

Use Arrays.sort()

var arr = [{"id":"165","name":"a"},{"id":"236","name":"c"},{"id":"376","name":"b"},{"id":"253","name":"f"},{"id":"235","name":"e"},{"id":"24","name":"d"},{"id":"26","name":"d"}];
arr.sort((a,b)=> Number(a.id) - Number(b.id));
console.log(arr);

2
  • @Anshul ie doest not support arrow functions, replace arrow function write function(a,b). And please mark answer as accepted if it helped you Commented Jul 3, 2018 at 13:02
  • Yes you are right It is working after changing arrow to function(a,b). @amrender singh
    – Anshul
    Commented Jul 5, 2018 at 13:16
1

var items = [
{
    "id": "165",
    "name": "a"
},
{
    "id": "236",
    "name": "c"
},
{
    "id": "376",
    "name": "b"
},
{
    "id": "253",
    "name": "f"
},
{
    "id": "235",
    "name": "e"
},
{
    "id": "24",
    "name": "d"
},
{
    "id": "26",
    "name": "d"
}];
items.sort((a, b) => Number(a.id) - Number(b.id));
console.log(items);

1
var items = [
{
    "id": "165",
    "name": "a"
},
{
    "id": "236",
    "name": "c"
},
{
    "id": "376",
    "name": "b"
},
{
    "id": "253",
    "name": "f"
},
{
    "id": "235",
    "name": "e"
},
{
    "id": "24",
    "name": "d"
},
{
    "id": "26",
    "name": "d"
}];

// for asscending
items.sort((a, b) => Number(a.id) - Number(b.id));
console.log(items);
// for descending
items.sort((a, b) => Number(b.id) - Number(a.id));
console.log(items);
1
  • isn't the logic for descending order should be -a+b instead of a+b Commented Jun 28, 2018 at 10:40
0

The numeric strings can be compared in many ways. Suppose the strings are a and b,

  1. a-b
  2. parseInt(a) - parseInt(b) - Reference - Java Script parseInt
  3. +a - +b - Reference - Unary + operator to convert string to number
  4. Number(a) - Number(b) - Reference - Javascript Global Number Object

var items = [{"id": "165","name": "a"},{"id": "236","name": "c"},{"id": "376","name": "b"},{"id": "253","name": "f"},{"id": "235","name": "e"},{"id": "24","name": "d"},{"id": "26","name": "d"}];

console.log(items.sort(function(a,b){ return parseInt(a.id)-parseInt(b.id) }));
console.log(items.sort(function(a,b){ return a.id-b.id }));
console.log(items.sort(function(a,b){ return +a.id - +b.id }));
console.log(items.sort(function(a,b){ return Number(a.id)-Number(b.id) }));

2
  • @Anshul FYI, you can also use the unary + to convert and compare them Commented Jun 28, 2018 at 10:49
  • Yes First one is working properly Because I was getting error on IE when using Arrow (=>) sign. Thanks @Vignesh Raja
    – Anshul
    Commented Jul 5, 2018 at 13:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.