2

I am trying to sort a list of array based on the name in alphabetical order which contain type, name, url and date. I retrieved the information from Browser.BookmarkColumns except for type.

Before:

Default Browser    Google     www.Google.com     14/12/2013
Default Browser    BBC        www.BBC.com        13/12/2015
Default Browser    Amazon     www.Amazon.com     11/11/2014

After:

Default Browser    Amazon     www.Amazon.com     11/11/2014
Default Browser    BBC        www.BBC.com        13/12/2015
Default Browser    Google     www.Google.com     14/12/2013

Here is what i have tried but it is not working.

 int j = mCur.getCount();
 String[] mType = new String[j];
 String[] mTitles = new String[j];
 String[] murls = new String[j];
 long[] date = new long[j];

 for (int q=0; q<(j-1); q++) {
                String a = (mTitles[q]).toLowerCase();
                String b = (mTitles[q+1].toLowerCase());

                char[] c = a.toCharArray();
                char[] d = b.toCharArray();

                String temp, temp2, temp3 = null;
                long temp4 = 0;

                int lenc = c.length;
                int lend = d.length;
                int min =0;
                int count =0;

                if (lenc < lend)
                    min = lenc;
                else
                    min = lend;

                    if (c[count] > d[count]) {
                        temp = mTitles[count];
                        temp2 = mType[count];
                        temp3 = murls[count];
                        temp4 = date[count];

                        mTitles[count] = mTitles[count + 1];
                        mType[count] = mType[count + 1];
                        murls[count] = murls[count + 1];
                        date[count] = date[count + 1];

                        mTitles[count + 1] = temp;
                        mType[count + 1] = temp2;
                        murls[count + 1] = temp3;
                        date[count + 1] = temp4;
                    } else if (c[count] == d[count]) {
                        for (int w = 1; w < min; w++) {
                            if (c[w] > d[w]) {
                                temp = mTitles[w];
                                temp2 = mType[w];
                                temp3 = murls[w];
                                temp4 = date[w];

                                mTitles[w] = mTitles[w + 1];
                                mType[w] = mType[w + 1];
                                murls[w] = murls[w + 1];
                                date[w] = date[w + 1];

                                mTitles[w + 1] = temp;
                                mType[w + 1] = temp2;
                                murls[w + 1] = temp3;
                                date[w + 1] = temp4;
                            }
                        }
                    }
            }
Dave
  • 117
  • 11

5 Answers5

3

First of all it would be much simplier task if instead of sorting 3 string arrays + long array You encapsulate all the fields and create a class (lets call it MyData) containing all four fields. Then you can use put all newly create objects in some collection (for example ArrayList).

So, when you have your ArrayList<MyData> you can easliy use Collections.sort passing both your list and implementation of Comparator<T> interface where all the sorting logic would be.

For example, if you want to sort whole list using only String title field it can look like this:

Comparator<MyData> with implemented compare function compare(MyData o1, MyData o2){return o1.title.compareTo(o2);

Than
  • 2,759
  • 1
  • 20
  • 41
3

Above answers are giving best example for efficient sorting Array list in java.

Before it please read description of above mentioned answer here

I just simplified above answer for your better understanding it gives exact output what u required.

ArrayList<UserContainer> userList = new ArrayList<>();
    userList.add(new UserContainer("www.Google.com", "Google", "14/12/2013"));
    userList.add(new UserContainer("www.BBC.com", "BBC", "13/12/2015"));
    userList.add(new UserContainer("www.Amazon.com", "Amazon", "11/11/2014"));

    Log.i("Before Sorting :", "==========================>>");
    for (UserContainer obj : userList) {
        System.out.println("Default Browser: \t" + obj.name + "\t" + obj.date + "\t" + obj.webSite);
    }

    Collections.sort(userList, new Comparator<UserContainer>() {
        @Override
        public int compare(UserContainer first, UserContainer second) {
            return first.name.compareToIgnoreCase(second.name);
        }
    });


    Log.i("After Sorting :", "==========================>>");
    for (UserContainer obj : userList) {
        System.out.println("Default Browser: \t" + obj.name + "\t" + obj.date + "\t" + obj.webSite);
    }

Model Class:

    public class UserContainer {
    public UserContainer(String webSite, String name, String date) {
        this.webSite = webSite;
        this.name = name;
        this.date = date;
    }

    public String webSite = "";
    public String name = "";
    public String date = "";
}
Community
  • 1
  • 1
Ravi Jaggarapu
  • 627
  • 3
  • 10
  • how can i sort it by date instead? date is long type not String. – Dave Dec 14 '15 at 12:02
  • Long.valueOf(date).compareTo(Long.valueOf(date2)) – Than Dec 14 '15 at 12:14
  • In Model Class, i declare "long date=0;". In my main compare method, Collections.sort(userList2, new Comparator() {public int compare(HistoryDetails date, HistoryDetails date2) {return Long.valueOf(date).compareTo(Long.valueOf(date2));}}); However, i have gotten a "cannot resolve method 'valueOf'" error. – Dave Dec 14 '15 at 12:37
  • 1
    // Place what ever formate you required ex : "dd-mm-yyyy" SimpleDateFormat f = new SimpleDateFormat("dd-mm-yyyy"); try { Long dateOne = f.parse(first.date).getTime(); Long dateSecond = f.parse(second.date).getTime(); return dateOne.compareTo(dateSecond); } catch (ParseException e) { e.printStackTrace(); } return 0; – Ravi Jaggarapu Dec 14 '15 at 12:37
  • Please the above code inside of 'compare()' method of Collections interface. Above i mention sort list by base on date value. If you required sorting by base on domain i am ready to give you that code also..! – Ravi Jaggarapu Dec 14 '15 at 12:39
  • One thing i want to say. I am just giving a way to develop easily. – Ravi Jaggarapu Dec 14 '15 at 12:43
  • Thanks for the help Ravi Hari and Than. Sincerely appreciate it. – Dave Dec 14 '15 at 13:06
0

My advice to create custom array list.

private ArrayList<UserContainer> userList=new ArrayList<UserContainer>();
UserContainer usercontainer=new UserContainer()
usercontainer.name=Amazon;
usercontainer.date=11/11/2014;
userList.add(usercontainer);


UserContainer usercontainer2=new UserContainer()
usercontainer.name=Google;
usercontainer.date=11/11/2014;
userList.add(usercontainer);

UserContainer usercontainer3=new UserContainer()
usercontainer.name=BBC;
usercontainer.date=11/11/2014;
userList.add(usercontainer);

Collections.sort(userList, new Comparator<UserContainer>() {
        @Override
        public int compare(UserContainer s1, UserContainer s2) {
            return s1.name.compareToIgnoreCase(s2.name);
        }
    });

Model:-

public class UserContainer {
public String name = "";
public String date = "";

}

I hope to help you.

jinkal
  • 1,622
  • 16
  • 21
0

Create a class and use comparator or comparable.

for further reference please check (How to sort an ArrayList in Java)

Community
  • 1
  • 1
ADattatrey
  • 252
  • 2
  • 10
0

Arrays.sort(stringArray); Its a nice way to sort.

I recommend you to create a Object for example 'BrowserStoredData' for each element of the list. With the strings required:

public class BrowserStoredData implements Comparable<BrowserStoredData> {

    String browserType;
    String browserName;
    String browserUrl;
    String browserDate;

    public BrowserStoredData(String browserType, String browserName,
            String browserUrl, String browserDate) {
        super();
        this.browserType = browserType;
        this.browserName = browserName;
        this.browserUrl = browserUrl;
        this.browserDate = browserDate;
    }

    public int compareTo(BrowserStoredData bsd) {
        return (this.browserName).compareTo(bsd.browserName);
    }

    @Override
    public String toString() {
        return browserType + "\t\t" + browserName + "\t\t" + browserUrl
                + "\t\t" + browserDate;
    }
}

With that object you easily can order a list of BrowserStoredData objects simply by using Collections.sort(yourList)

For example:

    BrowserStoredData bsd1 = new BrowserStoredData("Default Browser", "Google", "www.Google.com", "14/12/2013");
    BrowserStoredData bsd2 = new BrowserStoredData("Default Browser", "BBC", "www.BBC.com", "13/12/2015");
    BrowserStoredData bsd3 = new BrowserStoredData("Default Browser", "Amazon", "www.Amazon.com", "11/11/2014");

    List<BrowserStoredData> listBrowsers = new ArrayList<BrowserStoredData>();
    listBrowsers.add(bsd1);
    listBrowsers.add(bsd2);
    listBrowsers.add(bsd3);

    Collections.sort(listBrowsers);

    for (int i = 0 ; i < listBrowsers.size() ; i++){
        BrowserStoredData bsd = listBrowsers.get(i);
        System.out.println(bsd.toString());
    }

The exit will be:

Default Browser       Amazon      www.Amazon.com      11/11/2014
Default Browser       BBC         www.BBC.com         13/12/2015
Default Browser       Google      www.Google.com      14/12/201
VictorPurMar
  • 151
  • 9