1

I'm writing a test for a website (which uses Node.JS & Angular) using Selenium webdriver & Java. The site has list ordering functionality, where the items are listed in ascending / descending order, based on their names. The purpose here is to verify that the sorting function works properly.

The problem comes when the first letter of the element's name is one from the special Hungarian characters (á,é,ő, etc). When I use the website to sort the items, they will be placed right after their respective "normal" counterparts, but when I use Collections.sort() to manually sort the original, unordered elements extracted earlier, Java will move these elements after the last "normal" letter (z).

Here is an example, let's say our elements are displayed in the following order:

alma
körte
barack
árpa
kukorica

When I use the website's ordering method, the result will be the following:

alma
**árpa**
barack
körte
kukorica

However, Java's Collection.sort() will produce the following ordered list:

alma
barack
körte
kukorica
**árpa**

Technically, the two letters 'a' & 'á' represent the same letter and in our ABC they come right after each other.

My educated guess is Java uses ASCII codes of these characters to determine the order of the elements, where 'á' has higher value than 'a'. For some reason, Node.JS seems to use some sort of 'natural ABC' instead of the ASCII table.

My questions is: is there a way I can force the Collection.sort() to use the same method for sorting as the website?

Zoltán Györkei
  • 1,025
  • 5
  • 13
  • 21
  • 2
    Use Collators: https://docs.oracle.com/javase/tutorial/i18n/text/collationintro.html – Leos Literak Aug 23 '18 at 10:26
  • This seem to handle the initial problem, but now it presented another: we use 'i' at the end of the name of the city to express 'from that city', and here the there is also a difference: for some reason Java thinks Budapesti is greater than Budapest, while Node.JS says Budapest is greater than Budapesti (which is the proper way). Can I use the same collator to somehow overcome this new problem? – Zoltán Györkei Aug 23 '18 at 11:12
  • 1
    Java's string comparison is defined such that if one string is a prefix of the other, the shorter one is considered less than the longer one. That's typical of most systems and conventional dictionary order (at least in the U.S. and probably Western Europe). I'm surprised that Node.JS behaves differently, but then again, I know nothing about Node.JS. If you want Java to sort this way, it's probably possible write a comparator to do it. – Stuart Marks Aug 26 '18 at 17:36
  • simple code sample for using Collators, as suggested by Leos `Collator collator = Collator.getInstance(new Locale("hu", "HU")); Collections.sort(input, collator::compare);` – Erik Lievaart Nov 10 '19 at 11:00

0 Answers0