1

I must sort strings like "Text x.x.x(Text x)", "Text x.x.x", etc.(where x - number) in right way in Apex.

Something like that:

Input - ['Test 1.2(V 1000)', 'Test 1.10(V 1001)', 'Test 1.2(V 1001)', 'Test 1.2']

Need Output - ['Test 1.2', 'Test 1.2(V 1000)', 'Test 1.2(V 1001)', 'Test 1.10(V 1001)']

Result of List.sort - ['Test 1.10(V 1001)', 'Test 1.2', 'Test 1.2(V 1000)', 'Test 1.2(V 1001)')

  • 2
    Try with list.sort(); – itzmukeshy7 Oct 20 '16 at 9:47
  • @itzmukeshy7 list.sort() give another result, please see again – I.Kliavakin Oct 20 '16 at 12:37
  • 1
    You can use a regular expression to convert the string into its component parts (and types i.e, use Integer for the numbers) that you then hold as attributes of a class and then implement Comparable on the class using those values. Probably several hours of work if you are not familiar with this sort of code and if you include writing a test case that verifies the sorting. – Keith C Oct 20 '16 at 13:35
  • @I.Kliavakin How did you achieve the solution for this problem. I need to sort the alphanumeric versions. (1.8.44, 11.7.alpha, 9.x, Other, Assigned) – SFDC Aug 1 '19 at 5:45
2

+1 @itzmukeshy7

You can use sort() method of List to sort the elements of the List. I executed it in the Developer Console successfully.

List<String> lstStr = new List<String>{'Test 1.1.2(V 1000)', 'Test 1.1.1(V 1001)', 'Test 1.1.2(V 1001)', 'Test 1.1.2'};
lstStr.sort();
System.debug('Sorted List::'+   lstStr);
| improve this answer | |
  • I am sorry, I gave bad example, please see again what I need. – I.Kliavakin Oct 20 '16 at 12:36

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