1

I have this project where I have a variable called "Floor" as a String. My task is to sort the floor asc and desc way to show on the Recycler View.

My Json response contains the following element:

"floor": 1

I have this options asc and desc inside a spinner so I will show you the swicth case correspondent:

 case ASC:
       //Sort array list into Ascending order.
       Collections.sort(listModelsUa);
       listModelsUa.add(modelUA);
 case DESC:
       Collections.reverse(listModelsUa);
       listModelsUa.add(modelUA);

Note: My ModelUA implements Comparable and overrides the following method

String floor;

    @Override
        public int compareTo(@NonNull Object o) {
            int compareTo=((ModelUA )o).getFloor();
            /* For Ascending order*/
            return this.floor-compareTo;
        }

My question is,there are any other way to sort values inside an ArrayList using strings(maybe cast to Int) and displaying in the Recycler view?

2
  • Parse the strings into integer. For example by using Integer.parseInt(text);. Then you can sort them like integer. Commented Aug 3, 2018 at 8:54
  • 1
    Why does your ModelUA implement Comparable instead of Comparable<ModelUA>? Or, why do you have Object in your compareTo method? Use generics, not raw types. Commented Aug 3, 2018 at 8:55

2 Answers 2

3

You can use Stream.map() method to parse the Strings then sort the list.

This is how should be your code:

Collections.sort(listModelsUa.stream()
    .map(i -> Integer.parseInt(i))
    .distinct()
    .collect(Collectors.toList()));
Sign up to request clarification or add additional context in comments.

2 Comments

Note that instead of the lambda i -> Integer.parseInt(i) you can use a method reference Integer::parseInt.
@Zabuza Yes thank you for pointing it out, I know I posted it like this just to simplify things.
2

It is easy to convert string to integer as below. Then you can sort or reverse it.

int[] myIntArray = new int[myarray.length];

for (int i = 0; i < myarray.length; i++) {
    myIntArray[i] = Integer.parseInt(myarray[i]);
}
Collections.sort(myIntArray);

If you want to sort in alphabetical order using core java then use code as below.

    for (int i = 0; i < n; i++) 
    {
        for (int j = i + 1; j < n; j++) 
        {
            if (names[i].compareTo(names[j])>0) 
            {
                temp = names[i];
                names[i] = names[j];
                names[j] = temp;
            }
        }
    }
    System.out.print("Names in Sorted Order:");
    for (int i = 0; i < n - 1; i++) 
    {
        System.out.print(names[i] + ",");
    }

OR

 Collections.sort(Arrays.stream(myIntArray).stream()
.map(i -> Integer.parseInt(i))
.distinct()
.collect(Collectors.toList()));

Comments

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.