2

i have a list of sub family names which i am sorting using list.sort()

The subfamily names are like

familyname - 1 - parts
familyname - 2 - parts1
familyname - 3 - parts2
familyname - 11 - parts2

So when i sort, the result is like this

familyname - 1 - parts
familyname - 11 - parts2
familyname - 2 - parts1
familyname - 3 - parts2

We have familyname - 11 - parts2 coming after familyname - 1 - parts which is understandable as its sorting as a string.

Is there workaround over this? One way i am thinking is to split the string, store the number into a map with number and the family name. But this approach would fail, when i have another like familyname2 - 1 - parts

Any thoughts?

3 Answers 3

2

The bottom line is - you need a method that compares these 2 strings and says which should go before the other. Whether you'll do it with splits or something else...

  1. String.getCommonPrefix() looks interesting.
  2. So does indexOfAny('1234567890').
  3. or even 'familyname - 1 - parts'.substringBetween('-').trim() ;)

Just test the method extensively to be sure you're happy with results before proceeding.


Once you have a satisfying method of getting the number & the part after the number out, you have 2 options that both rely on it.

  1. Implement your own sorting algorithm (quicksort for example). quick Googling yields http://boards.developerforce.com/t5/Apex-Code-Development/Sort-a-list-of-SelectOptions-using-basic-quicksort/td-p/174637 or https://success.salesforce.com/ideaView?id=08730000000BrX6

  2. Put your data to a helper class, even if it'll be just a

    public class SortingHelper{
        String part1, part3; 
        Integer part2;
    }
    

    Have this class implement Comparable

0

Splitting and sorting on the parts is the only good solution I've ever found for this. You could use classes to represent the parts and store the data in a hierarchy of lists of classes to potentially make it easier to manage.

public class FamilyPart
{
  public String value {get; set;}
  public List<FamilyPart> parts {get; set;}
}

The top level list of family parts would contain 'familyname', 'familyname2' etc., then they'd each hold their respective sub family parts. This may or may not work with your scenario, but if you need to be able to sort and the like this could help you do that.

1
  • thanks LaceySnr, could you elaborate on the hierarchy of list of classes
    – Prady
    May 2, 2013 at 3:31
0

If they follow a particular pattern, you could use regular-expressions to determine your splits and sorts for you. Similarly, depending on how closely they match each other in name format, you could also use 'remove' to get the familyname1 parts using one string method pass, then do the same with the familyname2. I suggest you take a look at String Methods in the Apex Documentation along with Regular Expressions.

In essence, look for the 1st pattern, remove it from the string, then look for the second pattern and remove it next (you might be able to do this in one step with the right expression and loop). Finally, I'd check to see if anything is left that you may have missed and throw an exception that you can catch if it looks like its close to either of your patterns that perhaps should have caught it with some kind of method to handle it. If it passes, you should then have what you need to assemble two lists which you can sort in any order you wish.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .