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);
Related
This question already has answers here:
Fastest way to move first element to the end of an Array
(9 answers)
How to get the first element of an array?
(35 answers)
Closed 5 months ago.
I have an array which I have sorted from smallest integer to largest. The array data comes from backend and will be random numbers
// example array from backend
const arr = [400,30,10,-1]
const sortedArray = arr.sort((a, b) => a - b)
// [-1,10,30,400]
If the first index of the array is equal to -1 I want to remove it from the first position in the array and append it to the last position of the array.
For example if array is [-1, 10, 30, 400] I want to return [10,30,400,-1].
Edit: I am looking for the safest possible way and unsure to use splice(), filter() etc
shift the first element off the array and push it on the end.
const arr = [400,30,10,-1].sort();
if (arr[0] === -1) arr.push(arr.shift());
console.log(arr);
After your code you can check first element is -1 and then slice and push -1 to it
if(arr[0] == -1){
arr = arr.slice(1)
arr.push(-1)
}
I might have exaggerated the solution, but I am guessing it might help someone.
const beData = [400, 30, 10, -1];
const sortedData = beData.sort((a, b) => a - b);
const newArr = sortedData.reduce((prevValue, currValue) => {
if(currValue < 0) {
prevValue[1].push(currValue);
} else {
prevValue[0].push(currValue);
}
return prevValue;
}, [[], []]);
const result = [...newArr[0], ...newArr[1]];
console.log(result);
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));
This question already has answers here:
How to sort an array of integers correctly
(32 answers)
Closed 2 years ago.
I am training some javascript and i have seen a case in which i cant sort an object type array.
By the way if input array has only 1 digit items naive sort() function works as expected.
input is like --> [10,6,6,10,10,9,8,8,3,3,8,2,1,5,1,9,5,2,7,4,7,7]
when i run
var arr = Object.values(input).sort();
input becomes --> [1, 1, 10, 10, 10, 2, 2, 3, 3, 4, 5, 5, 6, 6,7, 7, 7, 8, 8, 8, 9,9]
where i miss the points, i am open to your ideas.
Thanks.
From the docs
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.
const input = [10,6,6,10,10,9,8,8,3,3,8,2,1,5,1,9,5,2,7,4,7,7]
// use a compareFunction: (a, b) => a - b
var arr = input.sort((a, b) => a - b)
More about the compareFunction, a and b are elements of the array. To decided if a is to be sorted before b, sort function uses the result of the compareFunction.
If the result of the compareFunction,
is -ve, a is placed before b
is 0, relative position of a and b will not be changed
is +ve, a is placed after b
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);
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