5

I am having real trouble with using stream and sorted to sort my ArrayList and hope someone can help out. The code uses Croatian words, but I don't think that will be a problem for someone who understands what I mean.

This is the ArrayList

ArrayList<Publikacija> listaPublikacija = new ArrayList<>();

listaPublikacija.add(prvaKnjiga);
listaPublikacija.add(drugaKnjiga);
listaPublikacija.add(prviCasopis);
listaPublikacija.add(drugiCasopis);

In my assignment I am supposed to sort these objects above by getCijena() which is double.

This is the best I got and it still doesn't sort it as it should...

listaPublikacija.stream().sorted((s1, s2) -> Double.compare(s1.getCijena(), s2.getCijena()));

Any kind of help or advice appreciated... I already made the sort in different ways, but it is neccessary to sort it via the sorted method in stream...

I'll post the class script below for easier understanding of the question above:

public Publikacija(String nazivKnjige, int godinaIzdanja, int brojStr, VrstaPublikacije vrstaPub, double cijenaPoStr, double cijena){
this.nazivKnjige= nazivKnjige;
this.godinaIzdanja = godinaIzdanja;
this.brojStr = brojStr;
this.vrstaPub = vrstaPub;
this.cijenaPoStr = cijenaPoStr;
if(getClass().equals(Casopis.class)){this.cijena= Casopis.CIJENA_PO_PRIMJERKU;}
else this.cijena = provjeraCijene(cijena(brojStr,vrstaPub,cijenaPoStr).doubleValue());
Stuart Marks
  • 112,017
  • 32
  • 182
  • 245
Ludi Dado
  • 63
  • 1
  • 1
  • 6
  • 1
    Does `listaPublikacija.stream().sorted(Comparator.comparing(e -> e.getCijena()));` end up with the same results? – Powerlord Mar 25 '15 at 21:13

5 Answers5

8

You are not storing the results of the sorted array back to collection or array. Stream operations do not mutate the underlying collection:

    List<String> names = Arrays.asList("Katka", "Martin", "John");

    Object[] sortedNames = names.stream().sorted(String::compareTo).toArray();

    System.out.println("array: " + Arrays.toString(sortedNames));
Crazyjavahacking
  • 7,955
  • 2
  • 27
  • 37
1
    Object[] sortirano = listaPublikacija.stream().sorted((s1, s2) -> Double.compare(s1.getCijena(), s2.getCijena())).toArray();

this worked, ty for the answer

Ludi Dado
  • 63
  • 1
  • 1
  • 6
1

Below code is used for to sort the your Publikacija object type Arraylist on behalf of "getCijena"

' Collections.sort(data, new Comparator<Publikacija>() {
            public int compare(Publ`enter code here`ikacija s1, Publikacija s2) {

                return s1.getCijena.compareTo(s2.getCijena);
            }
        });'
0
Publikacija[] sortedArray = listaPublikacija.stream()
   .sorted(Comparators.comparing(Publikacija::getCijena, Double::compareTo)
   .toArray();

First method reference extracts the identifying key, second provides the compare function.

DoNuT
  • 404
  • 1
  • 4
  • 20
0

You can also do it in that way:

listaPublikacija.stream().sorted(Comparator.comparingDouble(Publikacija::getCijena));
lczapski
  • 3,644
  • 3
  • 10
  • 26