Sorting Arraylist in Alphabetical Order (Case Insensitive)

Sorting arraylist in alphabetical order (case insensitive)

Custom Comparator should help

Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});

Or if you are using Java 8:

list.sort(String::compareToIgnoreCase);

How to sort alphabetically while ignoring case sensitive?

Here's a plain java example of the best way to do it:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Sorter {
String fruits[] = new String[7];
List<String> lst;

Sorter() {
lst = new ArrayList<String>();
// initialise UNSORTED array
fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach";
fruits[3] = "mango"; fruits[4] = "Apple"; fruits[5] = "pineapple";
fruits[6] = "banana";
}

public static void main(String[] args) {
Sorter srt = new Sorter();
srt.anyOldUnstaticMethod();

}
public void anyOldUnstaticMethod() {
Collections.addAll(lst, fruits);
System.out.println("Initial List");
for (String s : lst)
System.out.println(s);
Collections.sort(lst);
System.out.println("\nSorted List");
for (String s : lst)
System.out.println(s);
Collections.sort(lst, new SortIgnoreCase());
System.out.println("\nSorted Ignoring Case List");
for (String s : lst)
System.out.println(s);
}

public class SortIgnoreCase implements Comparator<Object> {
public int compare(Object o1, Object o2) {
String s1 = (String) o1;
String s2 = (String) o2;
return s1.toLowerCase().compareTo(s2.toLowerCase());
}
}
}

Java: How to sort an ArrayList alphabetically with case insensitive and with any number Strings at the end

Composition

Create your own Comparator<String>. Test for digits first, then and use String.CASE_INSENSITIVE_ORDER.

public class MyStringComparator implements Comparator<String> {
public int compareTo(String a, String b) {
//if both a and b are digits, maybe use String.CASE_INSENSITIVE_ORDER?
//if a is all digits return -1
//if b is all digits return 1
return String.CASE_INSENSTIVE_ORDER(a,b);
}
}

How to insert objects into an ArrayList in alphabetical order?

When you add the first city the size is zero. Your loop doesn't get executed.
The conditions provided are broken

for (int i=0; i<cityList.size();i++){
if (cityList.get(i).getName().toLowerCase().compareTo(city.getName().toLowerCase()) == 0){
//city is the same as one of the cities in the list (case insensitive)
//quit the loop (does not store two cities with same name)
return;
}else if (cityList.get(i).getName().toLowerCase().compareTo(city.getName().toLowerCase()) <0 && cityList.get(i+1).getName().toLowerCase().compareTo(city.getName().toLowerCase()) >0){
//city before is lexicographically lesser but city after is lexicographically more (case insensitive)
cityList.add(i+1, city);
//inserts at i+1, which is after the lexicographically smaller city, but is before the city which is lexicographically larger
return;
}

}
cityList.add(city);

Then you need to fix the conditions in your loop. First condition is ok, but consider the second condition:

cityList.get(i).getName().toLowerCase().compareTo(city.getName().toLowerCase()) <0 
&&
cityList.get(i+1).getName().toLowerCase().compareTo(city.getName().toLowerCase()) > 0

This does not need to be two parts. First check, if it is less than the current index, i, if it is less then you add it at that index. Don't check i+1

Simple way to sort strings in the (case sensitive) alphabetical order

If you don't want to add a dependency on Guava (per Michael's answer) then this comparator is equivalent:

private static Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() {
public int compare(String str1, String str2) {
int res = String.CASE_INSENSITIVE_ORDER.compare(str1, str2);
if (res == 0) {
res = str1.compareTo(str2);
}
return res;
}
};

Collections.sort(list, ALPHABETICAL_ORDER);

And I think it is just as easy to understand and code ...

The last 4 lines of the method can written more concisely as follows:

        return (res != 0) ? res : str1.compareTo(str2);

How to sort objects list in case insensitive order?

It looks like compareBy might be able to take a Comparator as an argument, see the documentation here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.comparisons/compare-by.html

Try:

places.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER, { it.name }))

Does Java have an alphabetical order like this?

  • You can use String.CASE_INSENSITIVE_ORDER to order strings in case-insensitive way.

  • If you also at the same time want to farther specify order of elements which current comparator considers as equal (like String.CASE_INSENSITIVE_ORDER would do for "Regen" and "regen") then you can use Comparator#thenComparing method and pass to it Comparator which would sort those equal elements like you want.

    • Assuming you would also want to order "Regen", "regen" as "regen", "Regen" (lower-case before upper-case) you can simply reverse their natural order with Comparator.reverseOrder().

So your code can look like:

regen.sort(String.CASE_INSENSITIVE_ORDER.thenComparing(Comparator.reverseOrder()));

Demo:

ArrayList<String> regen = new ArrayList<String>(
Arrays.asList("regelwidrig", "Regelwidrigkeit", "Regelzeit",
"Regen", "regen", "Regenabflussrohr",
"Regenanlage", "regenarm", "Regenbö", "Regenbogen")
);

regen.sort(String.CASE_INSENSITIVE_ORDER.thenComparing(Comparator.reverseOrder()));
System.out.println(regen);

Result: [regelwidrig, Regelwidrigkeit, Regelzeit, regen, Regen, Regenabflussrohr, Regenanlage, regenarm, Regenbogen, Regenbö]

(notice "Regen", "regen" ware swapped)

Sorting string value in a case-insensitive manner in Java 8

Try this

Comparator.comparing(Employee::getName, String.CASE_INSENSITIVE_ORDER)


Related Topics



Leave a reply



Submit