The following code is available:

public Product() {} public Integer getCod() { return cod; } public void setCod(Integer cod) { this.cod = cod; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Double getDiscount() { return discount; } public void setDiscount(Double discount) { this.discount = discount; } public Integer getBatchQuantity() { return batchQuantity; } public void setBatchQuantity(Integer batchQuantity) { this.batchQuantity = batchQuantity; } @Override public String toString() { return "Product{" + "cod=" + cod + ", name='" + name + '\'' + ", number=" + number + ", description='" + description + '\'' + ", price=" + price + ", discount=" + discount + ", batchQuantity=" + batchQuantity + '}'; } } 

And the following button code sorter:

 /** * Кнопка сортировщик */ @FXML public void onSortAction() { loadData(); message.setText(""); // сортировка по возрастанию названия и цены if (nameAsc.isSelected() == true && priceAsc.isSelected() == true) { List sortedProductList = productList.stream().sorted((o1, o2) -> -o1.getName().compareTo(o2.getName())).collect(Collectors.toList()); sortedProductList = productList.stream().sorted((o1, o2) -> -o1.getPrice().compareTo(o2.getPrice())).collect(Collectors.toList()); productList = new ArrayList<Product>(); productList = sortedProductList; } } 

I was able to sort by one field, how can I make it possible to sort this list by both fields?

    2 answers 2

    To sort by several fields, it is convenient to use comparators ( java.util.Comparator ) with their chains of calls of the type comparing().thenComparing().thenComparing()...

    Description of the thenComparing method

    Yes, and the code can be shortened without losing its clarity.
    here is my version of the function:

     public void onSortAction() { loadData(); message.setText(""); // сортировка по возрастанию названия и цены // сначала сравниваются названия, при равенстве названий сравниваются цены if (nameAsc.isSelected() == true && priceAsc.isSelected() == true) { List sortedProductList = productList.stream().sorted( Comparator.comparing(Product::getName).thenComparing(Product::getPrice) ).collect(Collectors.toList()); productList = sortedProductList; } } 
    • This is excellent, but I still haven't figured out the comparators ((((if there is a resource or an article, I would be grateful - Mishustiq
    • one
      To be honest, I myself have been learning the Java language not so long ago. I read about comparators in the book "Java 8 - The Complete Guide" (Herbert Shildt) and in the docks to Java. You can google for the phrase "comparators in Java", there are useful articles in extradition. - velial

    Update: pay attention to another answer , in my opinion it is clearer and more convenient.

    You need to decide which key is the main one when sorting, and first compare the objects by this key, and if the main keys are equal, compare the secondary keys. Below is the code under the assumption that the “key” field is the main key.

     public void onSortAction() { loadData(); message.setText(""); // сортировка по возрастанию названия и цены // сначала сравниваются названия, при равенстве названий сравниваются цены if (nameAsc.isSelected() == true && priceAsc.isSelected() == true) { List sortedProductList = productList.stream().sorted((o1, o2) -> { int compareNames = -o1.getName().compareTo(o2.getName()); return compareNames != 0 ? compareNames : -o1.getPrice().compareTo(o2.getPrice()); }).collect(Collectors.toList()); productList = sortedProductList; } } 
    • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin