-3
var countries = ["Algeria", "Canada", "Danmark", "Estonia"];
var search = "da";

Now I wish to sort this list so I get the following:

sortedCountries === ["Danmark", "Canada", "Algeria", "Estonia"]

I want DAnmark to come before CanaDA, because "da" is found earlier in that string. I do NOT wish to do a sort in ascending/descending order.

  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/198126/discussion-on-question-by-simon-sondrup-kristensen-sort-array-by-string-comparis). – deceze Aug 19 '19 at 09:45

1 Answers1

0

You can use Array.prototype.sort:

var countries = ["Algeria", "Canada", "Danmark", "Estonia"];
var search = "da";

console.log(countries.sort((a, b) => {
  a = a.toLowerCase().indexOf(search) + 1
  b = b.toLowerCase().indexOf(search) + 1
  
  if(!a && !b) return 0
  else if(!a) return 1
  else if(!b) return -1
  else return a - b
}))

We could write this as a utility function too:

var countries = ["Algeria", "Canada", "Danmark", "Estonia"]

const searchCountries = (a, s) => a.sort((a, b) => {
  s = s.toLowerCase()
  a = a.toLowerCase().indexOf(s) + 1
  b = b.toLowerCase().indexOf(s) + 1
  
  if(!a && !b) return 0 // don't sort if search fails for both
  else if(!a) return 1 // swap results since b has a value, but a doesn't
  else if(!b) return -1 // leave results since a has a value, but b doesn't
  else return a - b // otherwise, sort by indexOf result
})

console.log(searchCountries(countries, 'da'))
console.log(searchCountries(countries, 'a'))
console.log(searchCountries(countries, prompt()))
Kobe
  • 6,226
  • 1
  • 14
  • 35
  • 1
    `if(!a && !b) return 0` looks redundant. – zerkms Aug 19 '19 at 08:58
  • @zerkms It looks it, but it isn't, since I have to make all the other checks AFTER making that one, otherwise the ordering goes odd, and it allows for an early return :) – Kobe Aug 19 '19 at 08:59
  • At least with their input array it would return the same result :shrug: – zerkms Aug 19 '19 at 09:00
  • @zerkms True :P – Kobe Aug 19 '19 at 09:01
  • Not sure to be honest :P @SimonSondrupKristensen – Kobe Aug 19 '19 at 09:22
  • It was not me, but "code-only" answers are not encouraged on SO: they don't scale. OP checked the duplicates I provided and did not realise how they are similar. If they came with explanation and OP put some effort into it - we wouldn't need another answer that is helpful for a single person only. – zerkms Aug 19 '19 at 09:26
  • `if(!a && !b) return 0 // don't sort if search fails for both` --- this is really redundant... If neither matched - it does not matter what order they are. – zerkms Aug 19 '19 at 09:32
  • @zerkms How would I go about fixing it? Because if i remove it, the output will change. – Kobe Aug 19 '19 at 09:33
  • @Kobe https://jsfiddle.net/k4ezLbyc/ --- looks identical to me :shrug: – zerkms Aug 19 '19 at 09:34
  • Sure, but read the next line, now we're swapping the results, despite both of them not matching. – Kobe Aug 19 '19 at 09:35
  • @Kobe so what? How does it make the result invalid? Try to make up the input array that would not satisfy the requirement with that line removed. – zerkms Aug 19 '19 at 09:35