Sorting Strings using Collections.sort? - java

I'm attempting to sort two strings using the collection.sort() method, but I'm having issues understanding the logic of the implementation. Here is what I have so far. Are there any issues with my implementation?
Note: I want to sort them alphabetically:
"Apple" > "Orange"
Collections.sort(mailbox.getMessages() , (String a, String b) -> {
if (String.valueOf(a.charAt(0)) > String.valueOf(b.charAt(0))) {
return -1;
}
else if (String.valueOf(a.charAt(0)) <
String.valueOf(b.charAt(0))) {
return 1;
}
else {
return 0;
}
});

String implements a Comparable<String> which is implemented as a lexicographical comparison, in other words, by default "Apple".compareTo("Orange") < 0. So the default is sufficient.
Now Collections.sort has a variant that takes this comparator into account, you can thus simply use:
Collections.sort(mailbox.getMessages());
About your own implementation:
You shouldn't use String.valueof to cast back to a string: you can compare chars with the <, but you can't use this operator on Strings. Furthermore your implementation is not recursive: if the two first characters are equal, that doesn't mean the String's are equal per se, for instance "Apple" and "Ambiguous". So you would have to implement a more complex comparator.

You can't compare String with the symbol >. You can simply do :
Collections.sort(mailbox.getMessages(), (String a, String b) -> {
return Character.compare(a.charAt(0), b.charAt(0));
});
Note that this will sort according only to first character. If you want to sort on the entire string value lexographically then you can simply use Collections.sort(mailbox.getMessages()) as String already implements Comparable.

Related

TreeSet not arranging value in ascending order

I am trying to create a TreeSet to sort the strings which are inserted to be in an ascending order. I am using below code for entering values in TreeSet.
TreeSet<String> ts = new TreeSet<String>();
ts.add("#Test0");
ts.add("#Test1");
ts.add("#Test2");
ts.add("#Test3");
ts.add("#Test10");
ts.add("#Test4");
System.out.println("Tree set :: "+ts);
Output:
Tree set :: [#Test0, #Test1, #Test10, #Test2, #Test3, #Test4]
You've used the no-args TreeSet constructor. This means TreeSet will order its elements based on natural order. It's the way the objects compare themselves: It means the things you add must be of a type that implements Comparable<Self>. String does that: The String class is defined to implement Comparable<String>. However, the way strings compare themselves is lexicographically. 10 comes before 2 for the same reason that aa comes before b.
You have two routes available to fix this:
Don't put strings in there but some other object that implements Comparable and does it right. Perhaps a class Thingie {String name; int idx;}.
Pass a Comparator as first and only argument to your TreeSet class. Write code that determines that #Test10 comes before #Test2. Then, TreeSet uses this comparator to determine ordering and won't use the one built into strings.
Specify the Comparator to sort on the number part only. This removes all but the number portion, converts that to an integer and sorts on that.
TreeSet<String> ts = new TreeSet<String>(Comparator.comparing(
s -> Integer.valueOf(s.replace("#Test", ""))));
ts.add("#Test0");
ts.add("#Test1");
ts.add("#Test2");
ts.add("#Test3");
ts.add("#Test10");
ts.add("#Test4");
System.out.println(ts);
prints
[#Test0, #Test1, #Test2, #Test3, #Test4, #Test10]
This works for the shown example. You may need to modify it somewhat for more varied data. But it demonstrates the idea.
#Test10 comes before #Test2 because 1 comes before 2. That's how the default ordering of String works (String implements the interface Comparable to do this sorting).
To solve your issue you need to provide a custom Comparator to the TreeSet, and do the comparison by parsing the integer within the string:
TreeSet<String> ts = new TreeSet<String>(new Comparator<String>() {
#Override
public int compare(String s1, String s2) {
return Integer.parseInt(s1.substring(5)) - Integer.parseInt(s2.substring(5));
}
});
The comparator can be constructed using the static convenience method:
TreeSet<String> ts = new TreeSet<>(Comparator.comparing(s -> Integer.parseInt(s.substring(5))));
As #Jems noted in the comment, strings are sorted lexichographically, so "#Test10" will come before "#Test2". If could however, supply a custom Comparator to define the order you need. E.g., if you know all the strings will have the form of "#Test" followed by a number, you could extract this number and sort accordingly:
TreeSet<String> ts =
new TreeSet<>(Comparator.comparingInt(s -> Integer.parseInt(s.substring(5))));

Is there a class in Java which keeps duplicates but not order of data?

I am dealing with anagrams so I'm concerned only with the characters present in the string but not their order.
I searched for a suitable Collection class but in vain.
Can you please suggest any class that could help me to
keep duplicates but ignores order?
You can use a Map<Character,Integer> to count the number of occurrences of each character of a String. If the Maps generated for two Strings are equal, you'll know that the corresponding Strings are anagrams.
For example (here I used Map<Integer,Long> instead of Map<Character,Integer> since it was more convenient):
String one = "animal";
String two = "manila";
Map<Integer,Long> mapOne = one.chars ().boxed().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
Map<Integer,Long> mapTwo = two.chars ().boxed().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
System.out.println ("Is anagram? " + mapOne.equals(mapTwo));
Output:
Is anagram? true
You can use Google guava's HashMultiSet. The equals() method does exactly that:
Compares the specified object with this multiset for equality. Returns true if the given object is also a
multiset and contains equal elements with equal counts, regardless of
order. This implementation returns true if object is a multiset of the
same size and if, for each element, the two multisets have the same
count.
Instead of an ordered data structure, one can also dynamically sort the data.
As Unicode symbols, code points, are better than UTF-16 chars, I'll use Unicode ints instead:
int[] canonical(String s) {
return s.codePoints().sorted().toArray();
}
boolean isAnagram(String s, String t) {
return Arrays.equals(canonical(s), canonical(t));
}
boolean isAnagram(int[] s, String t) {
return Arrays.equals(s, canonical(t));
}

Sort array 2D by index Java

I am working on 2D array and I need to organize my array.
Explanation, my array is an Array String and it's containing in the header some double value, and I want to sort the array in function of the header.
The first thing I thought was to get the header and sort the header, get all the columns of the array in another array and comparate the value of the header ordered to each column index[0] and push to another array.
However, I thought there is a way to do it easily, but I don't know if there is, I saw the possibility to sort directly in the array and organize in function of this but I have no idea how to it.
Just in case :
Original array
String[][]myArray = {{3,Toto,30},{2,Tata,29},{1,Titi,13}};
Array expected
String[][]newArray = {{1,Titi,13},{2,Tata,29},{3,Toto,30}};
Open to proposal!
Thanks.
Edit : The header could be Double value.
If I understood you correctly, seems like this:
Arrays.stream(myArray)
.sorted(Comparator.comparingDouble(x -> Double.valueOf(x[0])))
.toArray(String[][]::new);
Of course you can do that in place too, via Arrays::sort and that Comparator
Previous answer just returned the original array when I tried it. Here's what worked for me:
String[][] myArray = {{"3","Toto","30"},{"2","Tata","29"},{"1","Titi","13"}};
Arrays.sort(myArray, new Comparator<String[]>() {
public int compare(String[] lhs, String[] rhs) {
try {
double lhs_value = Double.parseDouble(lhs[0]);
double rhs_value = Double.parseDouble(rhs[0]);
if (lhs_value < rhs_value) return -1;
if (lhs_value > rhs_value) return 1;
return 0; //else the two are equal
} catch (NumberFormatException e) {
//handle exception
return 0;
}
}
});
My output:
1 Titi 13
2 Tata 29
3 Toto 30
Here Arrays.sort (see Javadoc) takes in two parameters: an array you're sorting (myArray), and a Comparator (see Javadoc), which is an interface that allows comparison between two arbitrary types. Since {"3", "Toto", "30"} isn't a type you created, and is just a String[], we're going to make a Comparator<String[]> inline.
Comparators implement a function "compare" which takes in two elements, and returns -1, 0, or 1 to determine the ordering of the elements. Essentially "compare" gets called multiple times in the sorting process to precisely determine sorted order. Here's some pseudocode:
public int compare(Object a, Object b)
if (a comes "before" b) return -1
if (a is "equal" to b) return 0
if (a comes "after" b) return 1
If that isn't clear, you can learn more about the Comparator interface here:
https://www.geeksforgeeks.org/comparator-interface-java/
I know I used "before" and "after" earlier, which are a little fuzzy and non-rigorous. If you're more mathematically inclined, here's a good discussion of the Comparator interface:
https://math.stackexchange.com/questions/1400655/java-comparator-documentation-confused-about-the-terminology-total-order

Best way to sort a String ArrayList with numbers in it

I have an array list that is set up in this type of style...
4 dogs
10 cats
2 dogs
And I want to sort it so the ArrayList will be in the order of...
1 dogs
4 dogs
10 cats
Without using a comparator or another method, what is the shortest, easiest way to sort these by taking out the numbers (splitting the string by the space " "?), comparing them, and putting the sorted results back into the ArrayList? If you could attach your code snippet, it would be much appreciated.
The simplest way is to use comparator:
Collections.sort(list, new Comparator<String>() {
public void compare(String one, String two) {
return getNumber(one) - getNumber(two);
}
private int getNumber(String str) {
return Integer.parseInt(one.split(" ")[0]);
}
}
I really do not know what can be simpler.
Without an explicit Comparator, try using Java 8's Lambda feature:
//Assuming that the list variable is a string that looks like e.g. "1 10 2 5"
Arrays.sort(list.split(" "),
(String a, String b) -> { return Integer.valueOf(a).compareTo(Integer.valueOf(b)) };
);
//Assuming that the list variable is an array list of Strings
Collections.sort(list,
(String a, String b) -> { return Integer.valueOf(a).compareTo(Integer.valueOf(b)) };
);
You need to create a class have String field in that class and implement compareTo method and write comparison logic.
Use this class object in ArrayList and then use Collections.sort method.

Java TreeMap (comparator) and get method ignoring the comparator

public final Comparator<String> ID_IGN_CASE_COMP = new Comparator<String>() {
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
};
private Map< String, Animal > _animals = new TreeMap< String, Animal >(ID_IGN_CASE_COMP);
My problem is, how to use method get(id) ignoring the given comparator. I want the map to be order by Case Insensitive but, I want it to be case sensitive when I fetch the values by a given key.
I think the answer is easy. Implement your own comparator that does a case insensitive sort but does NOT return 0 for "A" and "a"... sort them too.
The issue is that your comparator returns 0 for the compare( "A", "a" ) case which means it is the same key as far as the map is concerned.
Use a comparator like:
public final Comparator<String> ID_IGN_CASE_COMP = new Comparator<String>() {
public int compare(String s1, String s2) {
int result = s1.compareToIgnoreCase(s2);
if( result == 0 )
result = s1.compareTo(s2);
return result;
}
};
Then all keys will go in regardless of case and "a" and "A" will still be sorted together.
In other words, get("a") will give you a different value from get("A")... and they will both show up in keySet() iterators. They will just be sorted together.
In a TreeMap, adding two keys a and b (in that order) so that compare(a, b) returns 0 will result in that the latest added entry (b) will overwrite the first one (a).
In your case, this means that there will never be any use for case insensitive get(id).
quoting http://java.sun.com/javase/6/docs/api/java/util/TreeMap.html
Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.
This is probably not what you want.
If the map is comparably small and you don't need to fetch the sorted entries very many times, a solution is to use a HashMap (or a TreeMap without explicitly setting the comparator), and sort the entries case-insensitively when you need them ordered.
You'll have to use two separate TreeMaps for that, with the same contents but different comparators.
maybe it'll do the job:
new Comparator<String>(){
public int compare(String s1, String s2)
{
String s1n = s1.toLowerCase();
String s2n = s2.toLowerCase();
if(s1n.equals(s2n))
{
return s1.compareTo(s2);
}
return s1n.compareTo(s2n);
}
};
}
you need a multimap: each entry of this multimap keeps the case insensitive keys and aanother map with the original keys as value.
There are many freely usable implementations of multimaps such as Common Collections, Google Collections, etc
In addition to all the other answers and agreeing, that it is impossible to have a single TreeMap structure with different comparators:
From your question I understand that you have two requirements: the data model shall be case sensitive (you want the case sensitive values when you use get()), the presenter shall be case insensitive (you want an case sensitive ordering, presentation is just an assumption).
Let's assume, we populate the Map with the mappings (aa,obj1), (aA,obj2), (Aa,obj3), (AA,obj4). The iterator will provides the values in the order: (obj4, obj3, obj2, obj1)(*). Now which order do you expect if the map was ordered case-insensitive? All four keys would be equal and the order undefined. Or are you looking for a solution that would resolve the collection {obj1, obj2, obj3, obj4} for the key 'AA'? But that's a different approach.
SO encourages the community to be honest: therefore my advice at this point is to look at your requirement again :)
(*) not tested, assumed that 'A' < 'a' = true.
Use floorEntry and then higherEntry in a loop to find the entries case-insensitively; stop when you find the exact key match.

Categories