Sort number part of a string? [duplicate] - javascript

This question already has answers here:
Sort Array Elements (string with numbers), natural sort
(8 answers)
Closed 5 years ago.
We have a bunch of SomeItems which have a field someId which is in the format (simplified / replaced with dummies):
abc.def1.<someNumber>
I have an array of SomeItems and have been sorting it like this:
let rows = someItems.sort((lhs, rhs) => lhs.someId > rhs.someId)
This pretty much just sorts it alphabetically, so this means that the order comes out a little weird:
abc.def1.1
abc.def1.1234
abc.def1.1235
abc.def1.2
abc.def1.234
I instead want it to be numerically sorted - like 1, 2, 234, 1234, etc.
What's a clean way to do this?

You could use String#localeCompare with options.
var array = ['abc.def1.1', 'abc.def1.1234', 'abc.def1.1235', 'abc.def1.2', 'abc.def1.234'];
array.sort(function (a,b) {
return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
});
console.log(array);

Related

JavaScript - sorting and getting rid of zeros [duplicate]

This question already has answers here:
Delete zero values from Array with JavaScript
(9 answers)
Closed 11 days ago.
I have a problem with sorting, my results sort correctly and zeros are displayed at the end. But I don't know how to make it sort and display sorted results but without zeros.
var data = [1,6,5,0,9,8,0];
data.sort((a, b) => {
return !a.num - !b.num || a.num - b.num;
})
``
output [1,5,6,8,9,0,0]
I would like the result:
output [1,5,6,8,9]
You could filter falsy values, like zero and sort the array after.
const
data = [1, 6, 5, 0, 9, 8, 0],
result = data.filter(Boolean).sort((a, b) => a - b);
console.log(...result);

Typescript .sort() skips over a value [duplicate]

This question already has answers here:
How to sort an array of integers correctly
(32 answers)
Closed 6 months ago.
Given the array of numbers
let el = [25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000];
The output of the following suggests that it ignores the value of 3000
let el = [25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000];
console.log(el.sort());
//Output [10000, 25000, 29000,3000, 33000, 40000,42000, 48000, 54000,57000, 79000, 86000]
So it did sort, but didn't move the 3000 at the 4th position. Why is this happening?
As VLAZ explained, By default, the sort() function sorts values as strings. if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
Because of this, the sort() method will produce incorrect results when sorting numbers.
You can fix this by providing a compare function: (a, b) => a - b)
compareFn(a, b) return value
sort order
> 0
sort a after b
< 0
sort a before b
=== 0
keep original order of a and b
source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description
e.g.
let el = [25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000];
console.log(el.sort((a, b) => a - b));

Sort array of arrays ( could not find duplicate for THIS instance ) [duplicate]

This question already has answers here:
How to sort 2 dimensional array by column value?
(14 answers)
How to sort multidimensional array in javascript
(4 answers)
How to sort an object array by date property?
(25 answers)
Closed 1 year ago.
I have an array for a timeline chart of Google Charts
So the array looks like follows
let data = [
['Outreach ICU Follow-up', '2021-01-25', 2021-02-01],
['Hospital Stay' , '2021-01-01', '2021-03-01'],
['ICU Stay' , '2021-01-02', '2021-01-25'],
['Outreach ICU Follow-up', '2021-02-20', '2021-03-01'],
['ICU Stay' , '2021-02-01', '2021-02-20'],
]
I lined up the date to see the chronological order. I need to sort this by the first data ( element 1 )
Cant use this because its for objects I think.
let ass = data.sort((x,y)=>y.dlk_assess_order-x.dlk_assess_order);
And this one is futile as-well.
var data = data.sort(function(a, b) { return a - b; });
How does one sort and array like this please?
Is there perhaps a lodash function?
As your question said, you simply sort by the element at position 1
let data = [
['Outreach ICU Follow-up', '2021-01-25', '2021-02-01'],
['Hospital Stay' , '2021-01-01', '2021-03-01'],
['ICU Stay' , '2021-01-02', '2021-01-25'],
['Outreach ICU Follow-up', '2021-02-20', '2021-03-01'],
['ICU Stay' , '2021-02-01', '2021-02-20'],
]
const sorted = data.sort((a,b) => a[1] - b[1]);
console.log(sorted);

Sorting numbers in array of objects [duplicate]

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Sort array of objects by string property value
(57 answers)
Closed 2 years ago.
I have an array of objects like the one below. Although, the order isn't in ascending or descending. Is there function that sorts the numbers in each object and replacing it into a new array or something similar?
let array = [
{
number: 16
},
{
number: 25
},
{
number: 20
},
{
number: 28
}
];
Yes, you can use array.sort() on a copy of your array.
example:
let newArray = [...array].sort((a, b) => a.number - b.number)
That will sort your array ascending.
If you want it descending just switch to b.number - a.number
More info on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Javascript's sort() not returning in ascending order [duplicate]

This question already has answers here:
How to sort an array of integers correctly
(32 answers)
Closed 7 years ago.
Javascript's sort() returns [19,20,2000,500] when applied to [20,19,500,2000]. Why is it considering 2000 to be less than 500?
var arr = [20,19,500,2000];
arr.sort();
arr;
// [19, 20, 2000, 500]
Javascript by default treat elements of array as string. So, when sorting 19, 720, 5 it'll get the first character from it and sorts in order. like 19, 5, 720.
var arr = [20,19,500,2000];
arr.sort(function(a, b) {
return a - b;
})
More Details
More on Sorting

Categories

Resources