I'm new to java and I recently wrote a program that sorts an array of strings in import java.util.Scanner;
public class
You can still use Arrays.sort method for this and just provide own customized Comparator:
Arrays.sort(name,
(String left, String right) ->
left.substring(1).compareTo(right.substring(1))
);
If you want to implement this on your own, note that comparator is supposed to return 0 if the paramaters are equal, negative number if the left is lower and positive number if the right is lower. So the comparator character by characters will look like:
Arrays.sort(name,
(String left, String right) -> {
// starting from index 1 and going until reaching end of strings
for (int i = 1; i < left.length() && i < right.length(); ++i) {
int diff = left.charAt(i)-right.charAt(i);
if (diff != 0)
return diff;
}
// if strings are equal so far, return that the lower is the shorter one
return left.length()-right.length();
}
);
collections framework gives the programmer access to prepackaged data structures as well as to algorithms for manipulating them. ref-tutorialspoint.com/java/java_collections.htm AND where you need trim()?Since you have already implemented the user input and displaying the values, I won't take that into consideration.
There are two things that need to be implemented:
These are two separate functions.
Take a look at the Comparator<T> interface. It lets you implement the way how objects are compared. The one and only method of the Comparator interface is the method compare(String o1, String o2). You only need to make sure that:
0. In our example below, Saab and Jaguar are the same, because we're trying to order only by the second character, which is an a for both.o1 is 'less' than o2, return -1. In our example, comparing Saab with Opel would return -1, because the a is 'less' than the p.o1 is 'greater' than o2, return 1. In our example, comparing Skoda with Jaguar would return 1, because the second character of Skoda (the k) is 'greater' than the second character of Jaguar (the a).By the way, in your original code, you used the method String.compareTo(), which is almost exactly the same as the Comparator<String>.compare(String, String) method.
class MyCustomComparator implements Comparator<String>() {
@Override
public int compare(String t, String t1) {
// We assume that the strings are not null
// We also assume that the length of the
// strings is at least 2.
if (t.charAt(1) < t1.charAt(1)) {
return -1;
}
else if (t.charAt(1) > t1.charAt(1)) {
return 1;
}
else {
return 0;
}
}
}
Once you have defined your own Comparator, the only thing you have to do is pass it to the method Arrays.sort(T[] a, Comparator<? super T> c), which accepts as first argument your array with the strings, and as second argument your own defined Comparator.
Example:
String[] array = new String[] {
"Saab",
"Opel",
"Skoda",
"Jaguar",
"Chevrolet"
};
// Result when ordering by second char:
// Saab, Jaguar, Chevy, Skoda, Opel
// Sort the array
Arrays.sort(array, new MyCustomComparator());
And if you really, really, really want to avoid the Arrays.sort() method, you can just reinvent the wheel:
/** Returns a new string array with the elements sorted */
String[] sort(String[] strings, Comparator<String> c) {
List<String> sortedArray = new ArrayList<String>();
for (String str : strings) {
int i;
for (i = 0; i < sortedArray.size(); i++) {
if (c.compare(str, sortedArray.get(i)) < 0) {
sortedArray.add(i, str);
break;
}
}
if (i == sortedArray.size()) {
sortedArray.add(strings[i]);
}
}
return sortedArray.toArray(new String[0]);
}
Alternatively, you can use your original code with these modifications:
void myCustomCompareTo(String s1, String s2) {
if (s1.charAt(1) < s2.charAt(1)) {
return -1;
}
else if (s1.charAt(1) > s2.charAt(2)) {
return 1;
}
else { // They're equal
return 0;
}
}
String[] sortMe(String[] strs) {
for (int i = 0; i < 5 - 1; i++) {
for (int j = 0; j < 5 - 1 - i; j++) {
if (strs[j].myCustomCompareTo(name[j + 1]) > 0) {
// Swap the two values
String temp = name[j];
strs[j] = strs[j + 1];
strs[j + 1] = temp;
}
}
}
return strs;
}
String class. You will read about the length() and the charAt(int) method. Especially length() is used often; I suggest you take that into account. Also, learning Generics might also help.i in a for loop, instead of x. You should consider doing so too.num.The arrows (->) Zbynek Vyskovsky - kvr000 is using, are part of lambda expressions. Lambda expressions are syntactic sugar, they make things easier to express (but not necessarily to read, especially for newcomers in the Java field).
For instance,
Arrays.sort(name,
(String left, String right) ->
left.substring(1).compareTo(right.substring(1))
);
is exactly the same as this:
Arrays.sort(name, new Comparator() {
@Override
public int compare(String left, String right) {
return left.substring(1).compareTo(right.substring(1));
}
);
But in fact, this is beyond the scope of this.
Arrays.sort()?
compareTomethod do it internally, means internally two string will be compared at index 1, 2,.. upto String length