Javascript how to sort array of objects by numeric value of the string

javascript sort array of objects by number
javascript sort array of objects by string property
javascript sort array of objects by key value
sort array of objects javascript
javascript sort array of numbers
angular 4 sort array of objects by property
javascript sort objects by property
typescript sort array of objects

Below is an example of my array of objects and I am supposed to sort these by name. The js normal sorting sorts as shown below, however my target is to sort by the numeric value, eg: "1 ex, 2 ex, 3 ex, 4 ex, 11 ex ..". Could someone help me how to achieve this?

[{id: 588, name: "1 ex"}
{id: 592, name: "11 ex"}
{id: 607, name: "2 ex"}
{id: 580, name: "3 ex"}
{id: 596, name: "4 ex"}]

You have to include the parse function in your compare function like this:

var compare = function(a, b) {
  return parseInt(a.name) - parseInt(b.name);
}

console.log(x.sort(compare));

Sorting an array of objects, Taking things one step further, lets say your array doesn't contain just simple numeric or string values, but objects with properties instead: ? 1. 2. 3. 4. 5. Introduction to JavaScript array sort method. The sort() method allows you to sort elements of an array in place. Besides returning the sorted array, the sort() method also sorted the array that you pass in. By default, the sort() method uses string Unicode code points to determine the order of the elements.


    arr = [{id: 588, name: "1 ex"},
    {id: 592, name: "11 ex"},
    {id: 607, name: "2 ex"},
    {id: 580, name: "3 ex"},
    {id: 596, name: "4 ex"}]
    
    result = arr.sort(function(a, b) { return parseInt(a.name) > parseInt(b.name) })
    console.log(result)

Quick Tip: How to Sort an Array of Objects in JavaScript, By default, the JavaScript Array.sort function converts each element in to sort an array of objects, whose values are either strings or numbers. The sort() method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort() method sorts the values as strings in alphabetical and ascending order.


Split and convert the numeric part to integer using parseInt() then you can use those values to sort the records:

var arr = [{id: 588, name: "1 ex"},
{id: 592, name: "11 ex"},
{id: 607, name: "2 ex"},
{id: 580, name: "3 ex"},
{id: 596, name: "4 ex"}];

arr.sort(function (a, b) {
  var aNum = parseInt(a.name);
  var bNum = parseInt(b.name);
  return aNum - bNum;
});

console.log(arr);

How to sort an array numerically in JavaScript, Ever try to sort an array of integers in JavaScript using .sort() and get a weird (​that is, alphabetically), so each integer actually got coerced into a string type. Since arrays are objects in JavaScript, their contents can still be  The employees array above is an array of objects with properties of different data types, from string, numeric, to a date string. The sort() method can be used to sort the array based on the values of one of these properties, such as sorting the array by name, age, or even when they retire. The basic idea is to modify the compare function so it compares the desired properties' values.


Try following

let arr = [{id: 588, name: "1 ex"},
{id: 592, name: "11 ex"},
{id: 607, name: "2 ex"},
{id: 580, name: "3 ex"},
{id: 596, name: "4 ex"}];

arr.sort((a,b) => a.name.split(" ")[0] - b.name.split(" ")[0]);

console.log(arr);

Array.prototype.sort(), The sort() method sorts the elements of an array in place and returns the sorted array. elements into strings, then comparing their sequences of UTF-16 code units values. In a numeric sort, 9 comes before 80, but because numbers are Objects can be sorted, given the value of one of their properties. The sort () method sorts an array alphabetically: The reverse () method reverses the elements in an array. By default, the sort () function sorts values as strings. This works well for strings ("Apple" comes before "Banana"). However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".


@user2004082, you can also try the below code to get the same as you want.

Try it online at http://rextester.com/GUPBC15345.

var arr = [{id: 588, name: "1 ex"},
{id: 592, name: "11 ex"},
{id: 607, name: "2 ex"},
{id: 580, name: "3 ex"},
{id: 596, name: "4 ex"}];

var map = {}
for(var item of arr) {
    map[item["name"]] = item;   
}

var arr = arr.map(function(item){return item["name"]}).sort(function(a, b){
    var a = parseInt(a.split(' ')[0]);
    var b = parseInt(b.split(' ')[0]);

    return a-b;
}).map(function(name){
    return map[name];
})

console.log(arr);

» Output

[ { id: 588, name: '1 ex' },
  { id: 607, name: '2 ex' },
  { id: 580, name: '3 ex' },
  { id: 596, name: '4 ex' },
  { id: 592, name: '11 ex' } ]

JavaScript Array sort() Method, The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort() method sorts the values as strings in alphabetical and ascending Return Value: The Array object, with the items sorted. You want to render this list, but first you want to order it by the value of one of the properties. For example you want to order it by the color name, in alphabetical order: black, red, white. You can use the sort () method of Array, which takes a callback function, which takes as parameters 2 objects contained in the array (which we call a


JavaScript Array Sort, By default, the sort() function sorts values as strings. The above example, array.​sort(), is not accurate, it will favor some numbers over Sorting Object Arrays. In our example above, we want to be able to sort an array of objects, whose values are either strings or numbers. If, however, you know that you will only be dealing with objects whose values are strings, you can tidy up the code a little using JavaScript’s localeCompare method.


JavaScript Array sort: Sorting an Array More Effectively, to sort arrays of numbers, arrays of strings, and arrays of objects by a property. the array elements in ascending order with the smallest value first and largest  TL;DR — Sort an array of numbers in ascending order using: myArray.sort((a, b) => a - b); Arrays in JavaScript are data structures consisting of a collection of data items. Because Javascript is not a typed language, Javascript arrays can contain different types of elements - strings,


Using JavaScript's sort Method for Sorting Arrays of Numbers , TL;DR — Sort an array of numbers in ascending order using: Javascript arrays can contain different types of elements - strings, numbers, undefined, etc. valueOf() method returns numeric values (such as the Date object). The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.