问题
I wanted to sort a list of names and I used compareTo method and I have names like this on my list
$urya, andy, prince, @rikesh,
expected output: andy, prince, $urya, @rikesh,
instead, I get: $urya @rikesh andy prince
What happens now is the name that starts with special characters come's first and then other names are sorted
Is there a way to push the names that starts with special characters to last.
if (this.firstName == name.firstName) {
return 0;
} else if (this.firstName == null) {
return 1;
} else if (name.firstName == null) {
return -1;
} else {
return this.firstName.compareToIgnoreCase(name.firstName);
}
回答1:
Java 8 steam API one liner
list.stream().filter(s -> s.replaceAll("[^a-zA-Z0-9]", "")).sorted()
回答2:
I think you want $
to be considered as S
. In this case, in the compareTo method, you have to replace $
with S
(similarly for others) and compare.
If you don't want to consider the $
itself(as the question says), you have to replace special characters with empty character.
回答3:
I have made a few modifications and as per your concern, i have ignored the special characters:
In the compareTo method, it has to look something like this:
if(this.name.replaceAll("[^a-zA-Z0-9]", "").compareTo(o.name.replaceAll("[^a-zA-Z0-9]", "")) >0)
return 1;
else if(this.name.replaceAll("[^a-zA-Z0-9]", "").compareTo(o.name.replaceAll("[^a-zA-Z0-9]", "")) < 0)
return -1;
and the output in console is :
before sort :[10-$urya, 10-andy, 10-prince, 10-@rikesh]
after sort :[10-andy, 10-prince, 10-@rikesh, 10-$urya]
Ignore 10, 10 is age in my Person Object.
Take a look at my classes, you will understand.
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class ComaprableCheck {
public static void main(String[] args) {
ArrayList<Person> person = new ArrayList<Person>();
Person p = new Person();
p.age =10;
p.name ="$urya";
person.add(p);
p = new Person();
p.age =10;
p.name="andy";
person.add(p);
p = new Person();
p.age =10;
p.name ="prince";
person.add(p);
p = new Person();
p.age =10;
p.name ="@rikesh";
person.add(p);
System.out.println("before sort :"+person);
Collections.sort(person);
System.out.println("after sort :"+person);
}
}
public class Person implements Comparable<Person> {
public int age;
public String name;
@Override
public int compareTo(Person o) {
if(this.age > o.age)
return 1;
else if(this.age<o.age)
return -1;
else
{
if(this.name.replaceAll("[^a-zA-Z0-9]", "").compareTo(o.name.replaceAll("[^a-zA-Z0-9]", "")) >0)
return 1;
else if(this.name.replaceAll("[^a-zA-Z0-9]", "").compareTo(o.name.replaceAll("[^a-zA-Z0-9]", "")) < 0)
return -1;
}
return 0;
}
public String toString(){
return this.age+"-"+this.name;
}
}
Below piece of code does the magic. It ignores the special characters just when they are comparing and returns the sorted list accordingly :
if(this.name.replaceAll("[^a-zA-Z0-9]", "").compareTo(o.name.replaceAll("[^a-zA-Z0-9]", "")) >0) return 1; else if(this.name.replaceAll("[^a-zA-Z0-9]", "").compareTo(o.name.replaceAll("[^a-zA-Z0-9]", "")) < 0) return -1;
来源:https://stackoverflow.com/questions/48784691/how-to-sort-without-considering-special-characters-in-java