0

I need to sort an array of objects by provided keys. Sorting must be case-insensitive and use provided alphabet. For example let's take initial data that looks like this:

var notOrdered = [{
  date: "11-12-2015",
  name: "Tomasz",
  age: 50,
  products: "FDGS",
  rate: 500
}, {
  date: "12-11-2015",
  name: "Łukasz",
  age: 54,
  products: "ŁBDGS",
  rate: 110
}, {
  date: "11-12-2015",
  name: "Jan",
  age: 24,
  products: "ŻDGS",
  rate: 1000
}, {
  date: "11-12-2015",
  name: "Łucja",
  age: 18,
  products: "AEBDGS",
  rate: 50
}];

var keys = ["date", "rate", "name"];
var directions = [true, false, true];
var alphabet = '01234567989aąbcćdeęfghijklłmnńoóprsśtuvwxyzźż'

So the result I'm looking for is:

var ordered = [{
  date: "11-12-2015",
  name: "Łucja",
  age: 18,
  products: "AEBDGS",
  rate: 50
}, {
  date: "11-12-2015",
  name: "Jan",
  age: 24,
  products: "ŻDGS",
  rate: 50
}, {
  date: "11-12-2015",
  name: "Tomasz",
  age: 50,
  products: "FDGS",
  rate: 500
}, {
  date: "12-11-2015",
  name: "Łukasz",
  age: 54,
  products: "ŁBDGS",
  rate: 110
}];

Using lodash's sortByOrder function var ordered = _.sortByOrder(notOrdered, keys, directions) takes care of sorting using provided keys and directions - one after another. And it works great. What I need now is to use provided alphabet order instead of the default one and make comparisons case-insensitive.

All the chars that are not listed in the provided alphabet should be compared the default way. I cannot use localCompare, because I need to support old IE and mobile browsers.

The question is: can I somehow make lodash's sortByOrder function to use custom alphabet and if so, how to do it?

zorza
  • 2,814
  • 3
  • 27
  • 41
  • you do know thart old IE is officiely no longer supported by microsoft – lordkain Jan 27 '16 at 12:54
  • I do know that. Unfortunately, my client doesn't care. Anyway, I couldn't make sortByOrder use localeCompare either. – zorza Jan 27 '16 at 12:55
  • alright, good luck with finding the solution! – lordkain Jan 27 '16 at 12:56
  • probaly you can use this .. if this module is whart u are using _.sortedIndexBy(array, value, [iteratee=_.identity]) – lordkain Jan 27 '16 at 12:58
  • and here is already an answer you can use http://stackoverflow.com/questions/28867721/how-to-sort-an-array-of-objects-using-a-related-property-from-objects-in-second – lordkain Jan 27 '16 at 13:00
  • By what field do you want to sort? – Johannes Jander Jan 27 '16 at 13:00
  • @lordkain Could you explain how .sortedIndexBy could help me here? I just cannot relate question from your link to my case. – zorza Jan 27 '16 at 13:10
  • @JohannesJander I want to sort by beys provided in `keys` array (`var keys = ["date", "rate", "name"]`) - one after another. – zorza Jan 27 '16 at 13:12

0 Answers0