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);
Answer from denis.solonenko on Stack OverflowCustom 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);
The simplest thing to do is:
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
So i have an arraylist where each index contains a book which consists of two strings, Author and Title.
How do i sort the list by authors alphabetically?
Any suggestions are appriciated, thanks!
java - How do I sort an ArrayList of strings by alphabetical order? - Stack Overflow
java - Sorting an ArrayList of Objects alphabetically - Stack Overflow
How can I make an array list in alphabetical order?
sorting - Sort alphabetically ArrayList of Strings in java - Stack Overflow
Use
Collections.sort(dogList, new Comparator<Dog>(){
public int compare(Dog d1, Dog d2){
return d1.getName().compareTo(d2.getName());
}
});
This will compare each dogs based on comparator. Comparator simply compares the names of dogs.
Update: For idiomatic java 8 style
Collections.sort(dogList, (d1,d2) -> d1.getName().compareTo(d2.getName()));
kindly follow this way, don't make code too much stuff, here i see you have mess up you code and it makes difficult to understand easily.
kindly follow this easiest way to sort Dog Object's list in sorting order(Ascending/Descending) way.
import java.util.ArrayList;
import java.util.Collections;
class Dog implements Comparable<Dog>{
String name;
Dog(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Dog o) {
return this.name.compareTo(o.getName()); // dog name sort in ascending order
//return o.getName().compareTo(this.name); use this line for dog name sort in descending order
}
@Override
public String toString() {
return this.name;
}
}
public class DogSort {
public static void main(String[] args) {
ArrayList<Dog> listDog = new ArrayList<Dog>();
listDog.add(new Dog("doggy"));
listDog.add(new Dog("aaa"));
listDog.add(new Dog("bbb"));
System.out.println(listDog);
Collections.sort(listDog);
System.out.println(listDog);
}
}
The sorting part can be done by implementing a custom Comparator<Vehicle>.
Collections.sort(vehiclearray, new Comparator<Vehicle>() {
public int compare(Vehicle v1, Vehicle v2) {
return v1.getEmail().compareTo(v2.getEmail());
}
});
This anonymous class will be used for sorting the Vehicle objects in the ArrayList on the base of their corresponding emails alphabetically.
Upgrading to Java8 will let you also implement this in a less verbose manner with a method reference:
Collections.sort(vehiclearray, Comparator.comparing(Vehicle::getEmail));
Although the question already has an accepted answer I would like to share some Java 8 solution
// if you only want to sort the list of Vehicles on their email address
Collections.sort(list, (p1, p2) -> p1.getEmail().compareTo(p2.getEmail()));
.
// sort the Vehicles in a Stream
list.stream().sorted((p1, p2) -> p1.getEmail().compareTo(p2.getEmail()));
.
// sort and print with a Stream in one go
list.stream().sorted((p1, p2) -> p1.getEmail().compareTo(p2.getEmail())).forEach(p -> System.out.printf("%s%n", p));
.
// sort with an Comparator (thanks @Philipp)
// for the list
Collections.sort(list, Comparator.comparing(Vehicle::getEmail));
// for the Stream
list.stream().sorted(Comparator.comparing(Vehicle::getEmail)).forEach(p -> System.out.printf("%s%n", p));
Like you said it Hospitals is not an Arraylist but just a String array (String[]) so you will need to use this:
List<String> list = Arrays.asList(etst);
Then you can use the following code to sort
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
If you want it ignoreCase:
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
Sort an array with Arrays.sort(Hospitals).
replace this line:
names.add(toUpperCase);
with this:
int index = names.size();
for (int i = 0; i < names.size(); i++) {
if (names.get(i).compareTo(toUpperCase) > 0) {
index = i;
break;
}
}
names.add(index, toUpperCase);
so, every time you have new string from user - you will insert it into proper position of your array list
this method is quite slow, but ok for home assignment
As suggested in the comments, the most simple way of maintaining a sorted data structure upon each insert is to use a TreeSet or any other data structure that maintains sorted order internally. Instead of declaring an ArrayList<String> you would simply need to modify your code to this:
Set<String> names = new TreeSet<>();
Scanner scan = new Scanner(System.in);
String name;
do {
System.out.println("Enter the next name: ");
name = scan.nextLine();
String toUpperCase = titleCase(name);
if(!toUpperCase.equals("Stop")){
names.add(toUpperCase);
}
} while(!name.equalsIgnoreCase("STOP"));
From Javadocs for TreeSet:
the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.