1

I have an String Array i have fileterd from a database with output like this in Java

[ABCD XYZ M1210, 
ABCD XYZ M149, 
ABCD XYZ M5130, 
ABCD XYZ N1420, 
ABCD XYZ T11299, 
ABCD XYZ S11044]

Im looking to sort my array as follows:

[ABCD XYZ M149, 
ABCD XYZ M1210, 
ABCD XYZ M5130, 
ABCD XYZ N1420,  
ABCD XYZ S11044,
ABCD XYZ T11299]

and it is the last element i specifically want

 ==> String theStringIWant = myStringArray.get(myStringArray.size() - 1);

What i need to dois first sort the letter after "XYZ" alphabetically and then sort the numerically after that so for example ABCD XYZ M1210 < ABCD XYZ M5130 as 5130 is greater than 1210.

Any help here would be much appreciated

*Any referencing to suitable libraries in java etc

Cheers

5
  • 1
    Have you seek for Collections#sort? Commented Nov 14, 2013 at 17:05
  • 1
    I would make my own Comparator to process the string in two parts. Commented Nov 14, 2013 at 17:06
  • @Pietu1998 Could you point to any similar comparator example for reference? Thanks Commented Nov 14, 2013 at 17:08
  • 1
    possible duplicate of Sort ArrayList of custom Objects by property Commented Nov 14, 2013 at 17:10
  • @RC Cheers I'll have a look Commented Nov 14, 2013 at 17:47

4 Answers 4

4
Arrays.sort(array, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        String stringPart1 = extractStringPart(s1); 
        String stringPart2 = extractStringPart(s2); 
        int result = stringPart1.compare(stringPart2);
        if (result == 0) {
            int intPart1 = extractIntPart(s1);
            int intPart2 = extractIntPart(s2);
            result = Integer.compare(intPart1, intPart2);
        }
        return result;
    }
});

The two extract methods are left as an exercise. Read the javadoc for String and/or Matcher to find out how you could do.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use Collections.sort to sort the contents.

In the mean while, write a comparator for comparison, like,

1 Write a Comparator

public class MyComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {

    String s1 = (String)o1;
    String s2 = (String)o2;
    //String part has 10 characters, It is fixed.
    String strPart1 = s1.substring(0,10); 
    int intPart1 = Integer.valueOf(s1.substring(10));

    String strPart2 = s2.substring(0,10);
    int intPart2 = Integer.valueOf(s2.substring(10));

    int strCompareResult = strPart1.compareTo(strPart2);
    if(0 == strCompareResult )
    {
        return intPart1 - intPart2;
    }
    else
    {
        return strCompareResult;
    }
}

}

2 Use Collections.sort and Comparator to complete the sorting

    List<String> results = Arrays.asList("ABCD XYZ M1210", "ABCD XYZ M149",
            "ABCD XYZ M5130", "ABCD XYZ N1420", "ABCD XYZ T11299",
            "ABCD XYZ S11044");

    System.out.println("Before sorting... ...");
    System.out.println(results);

    System.out.println("After sorting... ... ");
    Collections.sort(results, new MyComparator());
    System.out.println(results);

Output in Console:

Before sorting... ...

[ABCD XYZ M1210, ABCD XYZ M149, ABCD XYZ M5130, ABCD XYZ N1420, ABCD XYZ T11299, ABCD XYZ S11044]

After sorting... ...

[ABCD XYZ M149, ABCD XYZ M1210, ABCD XYZ M5130, ABCD XYZ N1420, ABCD XYZ S11044, ABCD XYZ T11299]

1 Comment

@Pietu1998 - Thanks for the correction. Changes are made accordingly. :)
1

and it is the last element i specifically want

The following program first extract the last integer part of your string element. And then return by comparing them.

List<String> results = Arrays.asList("ABCD XYZ M1210", "ABCD XYZ M149",
        "ABCD XYZ M5130", "ABCD XYZ N1420", "ABCD XYZ T11299",
        "ABCD XYZ S11044");

        Collections.sort(results, new Comparator<String>(){

            @Override
            public int compare(String o1, String o2) {
                Integer x1 = Integer.parseInt(o1.substring(o1.lastIndexOf(" ")+2, o1.length()));
                Integer x2 = Integer.parseInt(o2.substring(o2.lastIndexOf(" ")+2, o2.length()));

                return x1.compareTo(x2);

            }
        });

        System.out.println(results);

Output:

[ABCD XYZ M149, 
ABCD XYZ M1210, 
ABCD XYZ N1420, 
ABCD XYZ M5130, 
ABCD XYZ S11044, 
ABCD XYZ T11299]

1 Comment

@user1694873 notice the output carefully! it doesn't work. ABCD XYZ N1420, comes before ABCD XYZ M5130 which shouldn't.
1

A question can also be revers of it like sort number followed by alphabate i know this is revers of it but posting so anyone can get idea

expected output:B01 A02 A03 A04 A10 A11 C13 A20 A112


var items = ["A01", "A112","A02", "A03", "B01","C13","A04","A10", "A11", "A20"];
// console.log(   items.sort());
let obj={}
items.map((ele)=>{
   let y=ele.slice(1)
   // console.log(y);
   obj[parseInt(ele.slice(1))]=ele

})
console.log(obj);
for(let yy in obj)
{
console.log(obj[yy]);
}```

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.