0

Would Collections.sort() work in sorting by Make in a Car object Array List? I'm getting no error messages after adding the null there, but my goal is to sort them by Make, specifically, but I'm not entirely sure how to go about doing that.

public void readingFromFile(String file) throws FileNotFoundException //an object array that takes in string files
         {  
            try {
                File myFile = new File(file); //converts the parameter string into a file
                Scanner scanner = new Scanner(myFile); //File enables us to use Scanner
                String line = scanner.nextLine(); //reads the current line and points to the next one
                StringTokenizer tokenizer = new StringTokenizer(line, ","); //tokenizes the line, which is the scanned file

                 //counts the tokens 
                while (tokenizer.hasMoreTokens()){
                    String CarMake = tokenizer.nextToken(); //since car is in order by make, model, year, and mileage
                    String CarModel = tokenizer.nextToken();
                    int CarYear1 = Integer.parseInt(tokenizer.nextToken());
                    int CarMileage1 = Integer.parseInt(tokenizer.nextToken()); //converts the String numbers into integers
                    Car cars = new Car(CarMake, CarModel, CarYear1, CarMileage1); //since the car has a fixed order 
                    arraylist.add(cars); //add the cars to the unsorted array
                }
              scanner.close(); //close the scanner  
            } catch (FileNotFoundException f){
                f.printStackTrace();
                return;
            }
            arraylist2.addAll(arraylist);
            Collections.sort(arraylist2, null);
         }
Lost Soul
  • 75
  • 8
  • 1
    Did you intend to read more than one line? Also, why are you adding your list to a second list? This code makes no sense. – Elliott Frisch Mar 16 '19 at 09:22
  • 1
    A Comparator must be implemented. [Look at this example](https://stackoverflow.com/questions/16425127/how-to-use-collections-sort-in-java) – Yingkai Mar 16 '19 at 09:24
  • import java.util.Comparator; public class makeCompare implements Comparator{ public int compare(Car car1, Car car2) { // write comparison logic here like below , it's just a sample return car1.getMake().compareTo(car2.getMake()); } } – Lost Soul Mar 16 '19 at 09:30
  • @Yingkai Then, Collections.sort(arraylist2, new makeCompare()); would this work with a file with multiple lines of Make, Model, Year, and Mileage on a textfile? – Lost Soul Mar 16 '19 at 09:32
  • @ElliottFrisch yeah, it's an entire text file of lines. You're right though, I can fix that. – Lost Soul Mar 16 '19 at 09:33

2 Answers2

0

Use the streaming API:

sorted = arrayList2.stream().sorted(Comparator.comparing(Car::getMake));
D.R.
  • 20,268
  • 21
  • 102
  • 205
0

Since Java 8 the List has a sort method inherited by Collection. Additionally you can use the Comparator class to create a comparator very easy:

arraylist2.sort(Comparator.comparing(Car::getMake));

If you want to use multiple Parameters for your sort you can easily append them:

arraylist2.sort(Comparator.comparing(Car::getMake)
    .thenComparing(Car::getYear)
    // ...
);

If you are using a Java Version below Java 8 you have to implement the sort logic yourself in an Comparator or use an external library:

Collections.sort(arraylist2, new Comparator<Car>() {
    @Override
    public int compare(Car a, Car b) {
        return a.getMake().compareTo(b.getMake());
    }
});

For multiple parameters it would look like the following:

Collections.sort(arraylist2, new Comparator<Car>() {
    @Override
    public int compare(Car a, Car b) {
        int compareMake = a.getMake().compareTo(b.getMake());
        if (compareMake != 0) {
            return compareMake;
        }
        return a.getYear() - b.getYear();
    }
});
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56