Java – sort in array
•
Java
When I try to sort, I have a problem with my method:
This is what my code looks like:
public void sort() { boolean found=true; int i = 0; String temp; while(found) { found = false; for ( i = 0; i<cars.length-1; i++) { if (cars[i].compareToIgnoreCase(cars[i+1])> 0) {
I got so far, but I'm still not sure how to sort them?
Solution
attempt
Arrays.sort(cars);
If the car is a non-standard type, write your own comparator (name it COMP) and execute:
Arrays.sort(cars,comp);
Here's comparator interface documentation
Update or, as Mr. polywhirl said below, just implement the comparable interface as an automobile class
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码