简体   繁体   中英

Get ArrayList<String> value In ascending order

How do I retrieve value of arrayList in ascending order and convert it to string ?

private void fetchMessages(Map<String,Object> users) {

        ArrayList<String> messages = new ArrayList<>();

        //iterate through each user, ignoring their UID
        for (Map.Entry<String, Object> entry : users.entrySet()){

            //Get user map
            Map singleUser = (Map) entry.getValue();
            //Get phone field and append to list
            messages.add((String) singleUser.get("MessageBody"));

        }

        for (int i = 0; i<messages.size(); i++) {

            messagesN.add(i, messages.get(i));
            Log.d("Messages", String.valueOf(messagesN));
            messagesAdapter.addToStart(getTextMessage(), true);
        }

    }


 public Message getTextMessage() {
        return getTextMessage(getMessage());
    }

static ArrayList<String> messagesN = new ArrayList<String>();

What I wanted to do here is, append the messages value to messagesN and then return it in ascending order here -

  static String getMessage() {

            return messagesN.get(0);

        }

Currently it just returns the first value, But I want all the values in that array in ascending order.

Here is how to sort an Array List in Java in alphabetical order.

import java.util.Collections;



Collections.sort(messagesN);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM