0

I checked many samples but I couldn't figure out how could I implement Comparator or another method with my problem . I try to explain my problem clearly . I have an arrayList as follow : my Datamodel include an int as id and a String as name for each object ;

arrayList 1 = {id=0 - value= "name0", id=1 - value= "name1" , id = 2 - value = "name2"};

my second Arraylist also include an datamodel with more items but it include also the value of fisrt arrayList as follow :

arrayList 2 = {id = 100 - age = 20 - value ="name2",id = 101 - age = 21 - value ="name0",id = 102 - age = 18 - value ="name1"};

now I want to sort the second arrayList based on firstArrayList(value order) as follow :

orderedArrayList = {id = 101 - age = 21 - value ="name0",id = 102 - age = 18 - value ="name1",id = 100 - age = 20 - value ="name2"}

how Can achieve that?Can somebody help me to do this ? My android api is 21 also .

shayegan
  • 71
  • 7

1 Answers1

0

You mentioned you were trying to sort the second arrayList based on the firstArrayList. From what I am seeing in your code, you are trying to sort a list of tuples. A tuple is a list of element but it can be consider 1 element in a list. I recommend reinitialize both of your arrays as a list of tuple like this

//a list of tuples of 2 elements arrayList 1 = { [id=0 - value= "name0"], [id=1 - value= "name1"] , [id = 2 - value = "name2"]};

//a list of tuples of 3 elements arrayList 2 = { [id = 100 - age = 20 - value ="name2"], [id = 101 - age = 21 - value ="name0"], [id = 102 - age = 18 - value ="name1"]};

The code above is not the right syntax, I am just using pseudocode. Also, if you want to sort elements within the list, I suggest import this java library

import java.util.Arrays;

From there you can use arrayList.sort() (again not the right syntax). Let me know if that help!

Stephan
  • 155
  • 6