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.

1

1 Answer 1

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(","));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks its resolved my issue. But I got doubt how to handle if I have more conditions checks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.