2

I have a question, that i am getting a JSON response from a by calling a service, i am getting the name, location, position, email, phone number field. Each field is stored in its ArrayList. and i am showing these value in Listview.

Now i have to show the Name in sorted order, but other fields should also get arranged by corresponding name.

We can do by sorting by using Collection.sort(nameArrayList), but how other field will get affected?? how to do this in efficient way?? Any idea??

Thanks.

1

3 Answers 3

1

create a hashMap<String name ,pojo values) ; now read every name and store corresponding values location, position, email, phone number into pojo object .

map.put(name , pojo) ;

after inserting all values sort map by name . wherever using hashmap to populate list , iterate over keys and value and use them

When name is repeated

better to use ArrayList with your own comparator . go through this code snippet for more help

public class Main {

    String name = null;

    // 2011-12-11 11:30:20:0
    public static void main(String[] args) {

    ArrayList<Main> main = new ArrayList<Main>();
    main.add(new Main("CRohit"));
    main.add(new Main("DRohit"));
    main.add(new Main("ARohit"));
    main.add(new Main("12349"));
    main.add(new Main("BRohit"));
    main.add(new Main("ZRohit"));
    main.add(new Main("12345"));
    main.add(new Main("FRohit"));
    main.add(new Main("12348"));
    main.add(new Main(null));
    main.add(new Main(null));

    Collections.sort(main, new PojoComparator());
    for(Main m : main)
    {
        System.out.println(m.getName());
    }


    }

    public Main(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    class ChildProfileComparator implements Comparator<Main> {



    @Override
    public int compare(Main o1, Main o2) {

        String firstName = o1.getName();
        String secondName = o2.getName();

        if (firstName == null) {
            return +1;
        }
        if (secondName == null) {
            return -1;
        }

        if(isNumber(firstName) && isNumber(secondName))
        {
            double d1 = Double.parseDouble(firstName);
            double d2 = Double.parseDouble(secondName);

            return (d1>d2)?+1:-1;
        }

        if(isNumber(firstName))
        {
            return +1;
        }
        if(isNumber(secondName))
        {
            return -1;
        }
       return o1.getName().compareTo(o2.getName());
    }

    public boolean isNumber(String x)
    {
        if(x == null)
        {
            return false;
        }
        return x.matches("^-?\\d+(\\.\\d+)?$");
    }
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hello Shailendra, thanks for your response. I did the same way whatever you suggested, it works fine, but there is a case, when name will same for any two person then it will show the information about only one, because in HashMap key must be unique.
Oh right . i think now you should use ArrayList of pojo which contains names and other values , now create a Comparator which can sort elements by name inside this pojo . search for Comparator for more idea .
1

Checkout this Blog that has the complete explanation about the sorting of the content of the ListView.

Also you can get the source from Here(Note - haven't checked)

Comments

0

Instead of storing each field in its own array list, why not create a class to represent each object such that class will have fields for name, location, position, email, and phone number. When you parse the JSON create an instance of the above mentioned object, and put that into the ArrayList. Make the object Comparable by implementing a comparable interface.. Your compareTo function will simply dereference the name object of the input object and compares to the current object.

See here for more info: http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

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.