0

I'm writing test for Rails app with AngularJS where I want to check that sort on site works proper. On front-end clicking on sort button (orderBy:sort.column:sort.descending) this array:

arr = ["B kitty", "K kitty", "A kitty", "Z kitty", "b kitty", "L kitty", "S kitty", "q kitty", "c kitty"]

is sorted as:

sorted_arr = ["A kitty", "b kitty", "B kitty", "c kitty", "K kitty", "L kitty", "q kitty", "S kitty", "Z kitty"]

so that "b kitty" is infront of "B kitty".

But I cannot create the same sort method in Ruby to sort arr as expected, instead I always get "B kitty" infront of "b kitty".

Is there any way to achive this in my test?

3
  • What code are you using to sort?
    – simonwo
    Jun 19, 2014 at 8:42
  • Well in this case I would consider the AngularJS behavior unexpected, as capital letters do precede lower case ones in the ASCII and therefore also in the Unicode charset. If you desire caseless comparison you can use arr.sort(&:casecmp) in ruby, but that will leave the order of "b kitty" and "B kitty" unspecified as it should be expected. Could you be more specific on the ruby code you use to sort?
    – Patru
    Jun 19, 2014 at 8:49
  • Thanks guys for help, I tried to create custom sort, but seems that I need more practice in it :)
    – Vitalii.O
    Jun 19, 2014 at 10:06

4 Answers 4

1

arr.sort_by{|s| s.downcase} If you don't mind "b kitty" and "B kitty" having the same weight.

Otherwise you need a custom sort.

arr.sort do |s1, s2|
  if s1.downcase == s2.downcase
    s2 <=> s1
  else
    s1.downcase <=> s2.downcase
  end
end

Use the bang ! operator if you want to change arr itself.

1

You can do:

arr = ["B kitty", "K kitty", "A kitty", "Z kitty", "b kitty", "L kitty", "S kitty", "q kitty", "c kitty"]

arr.sort do |first, second|
  result = first.casecmp(second)
  result = second <=> first if result.zero?
  result
end
0
0

I think AngularJS just downcase the values and sort, and the result just happens to be that b kitty is in front of B kitty. The result isn't stable. (Correct me if I have wrong assumption).

If you want stable behavior in your Ruby code to get the unstable sort result from AngularJS, you can do it like this:

arr.sort{|a, b| (c = a.downcase <=> b.downcase) == 0 ? b <=> a : c }
2
  • Thanks, it works! Looks like this is the same that BroiSatse wrote, but with ternary and I like more your code.
    – Vitalii.O
    Jun 19, 2014 at 10:03
  • And yes, I think also that AngularJS sort result is unstable, there is no really preferences about b kitty and B kitty, but for tests I really need something stable. Actually I understand that comparing stable and unstable is not the best way.
    – Vitalii.O
    Jun 19, 2014 at 10:05
0
arr.sort_by{|x|[x.downcase,-x[0].ord]}
1
  • 2
    this might answer the question but its better to explain your answer in context to the asked question Jun 21, 2014 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.