0

I have list SortCriteria objects. object contains properties is sortColumn and sortAscending. I need to iterate list if sortAscending is true I need add "ASC" else I need add "DESC"

I have tried get the stream ascending list and stream of descending list and merging.

JSON:

"sort_criteria": [{

      "sortAscending": true,

      "sortColumn": "Category"

}]

JAVA LOGIC:

List<SortCriteria> listOfSortCriteria=webQueryRequest.getSortCriteria();
            List<SortCriteria> listOfAscending=listOfSortCriteria.stream().filter(e->e.getSortAscending().equals("true")).collect(Collectors.toList());
            List<SortCriteria> listOfDescending=listOfSortCriteria.stream().filter(e->e.getSortAscending().equals("false")).collect(Collectors.toList());
            orderByQuery=ORDER_BY+listOfAscending.stream().map(e->e.getSortColumn()+ " "+"ASC").collect(Collectors.joining(","))+","+listOfDescending.stream().map(e->e.getSortColumn()+" "+"DESC").collect(Collectors.joining(","));

Instead of getting the stream ascending list and stream of descending list and merging I want to do at one time need final constructed result.

Kraylog
  • 7,383
  • 1
  • 24
  • 35
madhusudhan
  • 370
  • 1
  • 4
  • 18
  • 1
    Possible duplicate of [How to use if-else logic in Java 8 stream forEach](https://stackoverflow.com/questions/38021061/how-to-use-if-else-logic-in-java-8-stream-foreach) – Feras Al Sous Jun 20 '19 at 11:47

1 Answers1

3

regardless of the original question, order of order columns is significant. by splitting into ascending and descending columns you change the order of order columns!
So, what you need to do is stream the original list and add the columns - each with its own direction:

orderByQuery = ORDER_BY + 
  listOfSortCriteria.stream().map(e->e.getSortColumn() + " " + (e->e.getSortAscending().equals("true") ? "ASC":"DESC"))
    .collect(Collectors.joining(","));
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47