I use underscore to sort the data, there is an array of data that must be sorted, but it is necessary to sort and not alphabetically and digit, but in a certain order, set in advance, now I use this sorting

sortedData = _.sortBy(sortedData, (item)=>{ return [item.lang, item.channel, item.status]; 

sorts by language and then, within this language, sorts by channel, but by channel its sorting, how can I add my own implementation of sorting there, there are only four channel variants.

  • the sort function with the insertion of the comparison function learn.javascript.ru/array-methods read the section your sorting order - nick_n_a
  • I know this, but how can I inject into my scheme, what could be the algorithm - J Mas
  • @JTan what values ​​have channels and what order is needed? - rony

1 answer 1

Suppose the channels are called a, b, c, d, the desired order is c, b, a, d then:

 sortedData = _.sortBy(sortedData, (item)=>{ return [item.lang, {a : 3, b : 2, c : 1, d : 4}[item.channel], item.status]}) 
  • it works, thank you, but another problem is that when the fifth sixth value is added, which should be sorted alphabetically after these four channels, for some reason they get ahead of these other four channels, how to make the rest be alphabetically after these four channels - J Mas