How to sort arraylist names in alphabetical order, but names that start with numbers should come last

I need to sort the list of names in alphabetical order, for this I use the interphase compiler to sort the names

  @Override
    public int compareTo(ContactModel another) {
        // TODO Auto-generated method stub
        return getname().compareTo(another.getname());
    }

Suppose this is my array of names

123vinish
23Sathya
24mahesh
Ranjith
Vipin
Bibin
Shine
Thomas

I need to sort this in a sequence where the names with numbers should be last

Bibin
Ranjith
Shine
Thomas
Vipin
123Vinish
23Sathya
24mahesh

How can we realize this

+4
source share
2 answers

String.compareTo(String)sort strings by ascii character value. You need to change the comparator. Example with a custom comparator:

List<String> al = new ArrayList<String>();
al.add("123vinish");
al.add("23Sathya");
al.add("24mahesh");
al.add("Ranjith");
al.add("Vipin");
al.add("Bibin");
al.add("Shine");
al.add("Thomas");

Comparator<String> nameComparator  = new Comparator<String>() 
{
    @Override
    public int compare(String value1, String value2)
    {
        if(Character.isDigit(value1.charAt(0))&&!Character.isDigit(value2.charAt(0)))
            return 1;
        if(Character.isDigit(value2.charAt(0))&&!Character.isDigit(value1.charAt(0)))
            return -1;
        return value1.compareTo(value2);
    }
};

Collections.sort(al, nameComparator);

System.out.println(al);

Output: [Bibin, Ranjith, Shine, Thomas, Vipin, 123vinish, 23Sathya, 24mahesh]

compareTo , Comparable<>, :

    @Override
    public int compareTo(ContactModel anotherValue)
    {
        if(Character.isDigit(this.getname().charAt(0))&&!Character.isDigit(anotherValue.getname().charAt(0)))
            return 1;
        if(Character.isDigit(anotherValue.getname().charAt(0))&&!Character.isDigit(this.getname().charAt(0)))
            return -1;
        return this.getname().compareTo(anotherValue.getname());
    }

EDIT: , 2a - 23, String. :

Comparator<String> nameComparator  = new Comparator<String>() 
{
    @Override
    public int compare(String value1, String value2)
    {
        for(int i = 0; i < Math.min(value1.length(), value2.length()); i++)
        {
            //value1 is digit and value2 is not
            if(Character.isDigit(value1.charAt(i)) && !Character.isDigit(value2.charAt(i)))
                return 1;
            //value2 is digit and value1 is not
            else if(Character.isDigit(value2.charAt(i)) && !Character.isDigit(value1.charAt(i)))
                return -1;
            //both are digits with different size
            else if(Character.isDigit(value1.charAt(i)) && Character.isDigit(value2.charAt(i)) && Integer.valueOf(value1.charAt(i))!=Integer.valueOf(value2.charAt(i)))
                return value1.compareTo(value2);
            //both are no digits
            else if(!Character.isDigit(value1.charAt(i)) && !Character.isDigit(value2.charAt(i)))   
                return value1.compareTo(value2);
            //loop again if they are both digits with the same value
        }
        return value1.compareTo(value2);
    }
};

2:

. compareTo String ascii . ContactModel.

@Override
public int compareTo(ContactModel anotherValue) {
    int len1 = this.getname().length();
    int len2 = anotherValue.getname().length();
    int lim = Math.min(len1, len2);
    char v1[] = this.getname().toCharArray();
    char v2[] = anotherValue.getname().toCharArray();

    int k = 0;
    while (k < lim) {
        //Do the trick here! If char is a digit, add 75.
        char c1 = Character.isDigit(v1[k]) ? ((char)(v1[k]+75)) : v1[k];  
        char c2 = Character.isDigit(v2[k]) ? ((char)(v2[k]+75)) : v2[k];
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2;
}
+8

, 2-

@Override
public int compareTo(ContactModel another) {
    // TODO Auto-generated method stub
    boolean lhsStartsWithDigit = Character.isDigit(getStartLetter().charAt(0));
    boolean rhsStartsWithDigit= Character.isDigit(another.getStartLetter().charAt(0));
    if((lhsStartsWithDigit && rhsStartsWithDigit) || (!lhsStartsWithDigit && ! rhsStartsWithDigit))
        return getStartLetter().compareToIgnoreCase(another.getStartLetter());
    else if(lhsStartsWithDigit)
        return 1;
    else
        return -1;

}

?

0

Source: https://habr.com/ru/post/1532777/


All Articles