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 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 Commented Jun 28, 2018 at 10:23

5 Answers 5

4
items.sort((a,b)=>a.id-b.id)
Sign up to request clarification or add additional context in comments.

1 Comment

This won't work on older browsers, but is widely supported.
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 Comments

@Anshul ie doest not support arrow functions, replace arrow function write function(a,b). And please mark answer as accepted if it helped you
Yes you are right It is working after changing arrow to function(a,b). @amrender singh
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);

Comments

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 Comment

isn't the logic for descending order should be -a+b instead of a+b
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 Comments

@Anshul FYI, you can also use the unary + to convert and compare them
Yes First one is working properly Because I was getting error on IE when using Arrow (=>) sign. Thanks @Vignesh Raja

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.