0

This is array i have:

var myArray = [
  '<@424507945156784498> - 152,800$',
  '<@381223410610501732> - 100$',
  '<@224451506344606852> - 74,424$',
  '<@101441124537903160> - 65,100$'
];

And this what i want:

var newArray = [
  '<@424507945156784498> - 152,800$',
  '<@224451506344606852> - 74,424$',
  '<@101441124537903160> - 65,100$'
  '<@381223410610501732> - 100$',      
];

I tried to use this method - Javascript : natural sort of alphanumerical strings but its sorting by id (<@424507945156784498>) how can i sort by money value?

Jim D.
  • 45
  • 7
  • Post the code that you attempted. – Prerak Sola Jun 29 '18 at 12:53
  • It's not sorting by id. It's sorting by the whole string because each entry in the array is one value... The SO link you attached strips non numeric characters (essentially) and sorts the array. – Adrian Jun 29 '18 at 12:54

4 Answers4

2

Split and replace - the sort will sort the existing array - if you need to not mutate it, you need to copy it to another array:

var myArray = [
  '<@424507945156784498> - 152,800$',
  '<@381223410610501732> - 100$',
  '<@224451506344606852> - 74,424$',
  '<@101441124537903160> - 65,100$'
];
function toNum(str) {
  return +str.split(" - ")[1]  // get the amount
    .replace(/[^\d]/g,"");     // remove all non-numeric
}
myArray.sort(function(a,b) {
  return toNum(b)-toNum(a);    // numeric sort in situ
});
console.log(myArray)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

Uses the map-sort-map idiom:

  1. map new data to sort by onto the existing element
  2. sort by added data
  3. map to remove the element that was sorted by

var myArray = [
  '<@424507945156784498> - 152,800$',
  '<@381223410610501732> - 100$',
  '<@224451506344606852> - 74,424$',
  '<@101441124537903160> - 65,100$'
];
    
console.log(
  myArray.map(e => [e, e.split(' - ')[1].replace(/[^0-9]/g,'')])
         .sort((a, b) => b[1] - a[1])
         .map(e => e[0])
);
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
0

Try the following expression:

myArray.sort((x,y)=>x.replace(/,/g,"").match(/(\d+)\$/)[1]*1 < y.replace(/,/g,"").match(/(\d+)\$/)[1]*1)

Explanation:

x.replace(/,/g,"").match(/(\d+)\$/)[1]*1

This expression removes the comma and then matches the number followed by a $. This is done for x and y used in the sort method.

var myArray = [
  '<@424507945156784498> - 152,800$',
  '<@381223410610501732> - 100$',
  '<@224451506344606852> - 74,424$',
  '<@101441124537903160> - 65,100$'
];

console.log(myArray.sort((x,y)=>x.replace(/,/g,"").match(/(\d+)\$/)[1]*1 < y.replace(/,/g,"").match(/(\d+)\$/)[1]*1))
R. Schifini
  • 9,085
  • 2
  • 26
  • 32
0

Simply, extract the price value and sort by it:

var myArray = [
  '<@424507945156784498> - 152,800$',
  '<@381223410610501732> - 100$',
  '<@224451506344606852> - 74,424$',
  '<@101441124537903160> - 65,100$'
];

var result = myArray.sort((a,b) => {

  var [priceA, priceB] = [a,b].map(i => parseInt(i.split('-')[1].trim().replace(/\D/g,'')))

  return priceB - priceA;

});

console.log(result);
Leonid Pyrlia
  • 1,594
  • 2
  • 11
  • 14