3

Am having the following JSONArray which am trying to sort it. So, for that am converting my JSONArray into ArrayList then am sorting them and converting back into JSONArray.

Please find the initial JSONArray (which is not in sorting order).

[  
   {  

      "code":"TE-7000-8003-W",
      "id":"13342",

   },
   {  
      "code":"TE-7000-8003",
      "id":"13163",
   },
   {  
      "code":"TE-7000-8003-WK",
     "id":"11573",
   },
   {  
      "code":"TE-7000-8003S",
      "id":"11565",

   },
   {  
      "code":"TE-7000-8003-K",
      "id":"11557",
   }
]

Please find my below code which am converting my JSONArray into ArrayList and sorting them.

Item item=null;
List<Item> newItemList = new ArrayList<Item>();
for (int i=0;i<resultJSONArray.length();i++) {
    JSONObject jobj = resultJSONArray.getJSONObject(i);
    item = new Item();
    item.setId(jobj.optString("id"));
    item.setCode(jobj.optString("code"));
    newItemList.add(item);
}

 newItemList
  .stream()
  .sorted((object1, object2) -> object1.getCode().compareTo(object2.getCode()));

Iterator<Item> itr = newItemList.iterator();
while(itr.hasNext()) {
    Item item1=itr.next();
    System.out.println("Item----->"+item1.getCode());
}

Following is the output am getting which is not sorted order

Item----->TE-7000-8003-W
Item----->TE-7000-8003
Item----->TE-7000-8003-WK
Item----->TE-7000-8003S
Item----->TE-7000-8003-K

Am expecting the result like below :

Item----->TE-7000-8003
Item----->TE-7000-8003S
Item----->TE-7000-8003-K
Item----->TE-7000-8003-W
Item----->TE-7000-8003-WK
2
  • 4
    You're not assigning the result of the sorting operation to anything. So you're printing the original, not sorted list. Just use list.sort() if you want to sort in-place. Commented Sep 19, 2018 at 14:09
  • 2
    Use newItemList.sort(yourComparator) to sort a list instead. Commented Sep 19, 2018 at 14:10

2 Answers 2

8

When you create a stream and use sorted you don't change the actual list. So you could do the following:

List<Item> sortedItemList =newItemList
.stream()
.sorted((object1, object2) -> object1.getCode().compareTo(object2.getCode()))
.collect(Collectors.toList());

or better sort the list with the sort method

newItemList
.sort((object1, object2) -> object1.getCode().compareTo(object2.getCode()));

And you could use Comparator.comparing(Item::getCode) to replace the comparator

newItemList
.sort(Comparator.comparing(Item::getCode));
Sign up to request clarification or add additional context in comments.

1 Comment

And the comparator could be simplified to Comparator.comparing(Item::getCode)
3

Use a simple Comparator to apply on your list just like that ;

newItemList
.sort((firstObj, secondObj) -> firstObj.getCode().compareTo(secondObj.getCode()));

Or more simple ;

newItemList.sort(Comparator.comparing(Item::getCode)); //dont forget to write getter method of Code variable.

2 Comments

Item::getCode can't possibly be a Comparator. You probably mean Comparator.comparing(Item::getCode)
Thanks for your attention @JBNizet :)

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.