0

I have four ArrayLists. I want to sort one of them alphabetically with case ignored and do the same changes in the other three ArrayLists.

 ArrayList<String> arrPackage = new ArrayList<>();
 ArrayList<String> arrPackageDates = new ArrayList<>();
 ArrayList<String> arrPackageDuration = new ArrayList<>();
 ArrayList<String> arrPackageFileSize = new ArrayList<>();
 // Code to add data to ArrayLists (data is not coming from sqlite database)
 ...
 // Code to sort arrPackage alphabatically with case ignored
  Collections.sort(arrPackage, new Comparator<String>() {
                        @Override
                        public int compare(String s1, String s2) {
                            return s1.compareToIgnoreCase(s2);
                        }
                    });

but how do I know which indexes were changed?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ali
  • 839
  • 11
  • 21
  • What exactly do you need to know regarding which indices were changed? – Tim Biegeleisen May 05 '17 at 04:10
  • I just need to do the same changes in the other three ArrayLists for that I think I need to know which indexes were interchanged in the first ArrayList? – Ali May 05 '17 at 04:12
  • 2
    Then you should create a wrapper object called `Package`, which contains fields for the name, date, duration, and file size. Sort packages together. As an aside, if this data is somehow coming from a database, then you should probably be doing the sorting there. – Tim Biegeleisen May 05 '17 at 04:13
  • So basically you need the data to be sorted like a table? So I would recommend storing the data in a SQLite Database, and then sorting it, and then deleting it later if you don't need the data saved.. Simply use the ORDERBY property to fetch sorted rows.. Take a look here to sort a table http://stackoverflow.com/a/17187017/3286614 – Rachit May 05 '17 at 04:14
  • Create another ArrayList with integer values 0..n and sort it with a comparator comparing the values in arrPackage, accessing them via the indices in the additional list. – laune May 05 '17 at 04:15

3 Answers3

2

One approach would be to create a wrapper object Package which contains the four types of metadata which appears in the four current lists. Something like this:

public class Package {
    private String name;
    private String date;
    private String duration;
    private String fileSize;

    public Package() {
        // can include other constructors as well
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // other getters and setters
}

Then sort using a custom comparator which works on Package objects:

List<Package> packages = new ArrayList<>();

Collections.sort(packages, new Comparator<Package>() {
    @Override
    public int compare(Package p1, Package p2) {
        String name1 = p1.getName();
        String name2 = p2.getName();

        return name1.compareToIgnoreCase(name2);
    }
});

As a general disclaimer, the above operation would most likely be performed must more efficiently in a database. So if your data is ultimately coming from a database, you should try to do such heavy lifting there.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

A simple easy way would be backing up your ArrayList.

ArrayList<String> backupPackage = arrPackage;

And then use your code to sort the array. Then use a for loop to compare the two arrays.

for (int i = 0; i < backupArray.size(); i++) {
    if (!aar.get(i).equals(aar.get(i))) { // then the item has been changed
        // ... YOUR CODE

        // at this point you know which indexes have been changed and can modify your other arrays in any way you need
    }
}
JFreeman
  • 974
  • 1
  • 10
  • 26
0

I used this approach:

                    ArrayList<String> backupPackage = new ArrayList<>();
                    ArrayList<String> backupPackageDates = new ArrayList<>();
                    ArrayList<String> backupPackageDuration = new ArrayList<>();
                    ArrayList<String> backupPackageFileSize = new ArrayList<>();
                    for(int j=0;j<arrPackage.size();j++) {
                        backupPackage.add(arrPackage.get(j));
                    }
                    for(int j=0;j<arrPackageDates.size();j++) {
                        backupPackageDates.add(arrPackageDates.get(j));
                    }
                    for(int j=0;j<arrPackageDuration.size();j++) {
                        backupPackageDuration.add(arrPackageDuration.get(j));
                    }
                    for(int j=0;j<arrPackageFileSize.size();j++) {
                        backupPackageFileSize.add(arrPackageFileSize.get(j));
                    }
                    Collections.sort(arrPackage, new Comparator<String>() {
                        @Override
                        public int compare(String s1, String s2) {
                            return s1.compareToIgnoreCase(s2);
                        }

                    });
                    int newindex;
                    for(int i=0; i<backupPackage.size(); i++) {
                        newindex = backupPackage.indexOf(arrPackage.get(i));
                        if(newindex != i) {
                            arrPackageDates.set(i, backupPackageDates.get(newindex));
                            arrPackageDuration.set(i, backupPackageDuration.get(newindex));
                            arrPackageFileSize.set(i, backupPackageFileSize.get(newindex));
                        }
                    }
                    backupPackage.clear();                    
                    backupPackageDuration.clear();     
                    backupPackageDuration.clear();
                    backupPackageFileSize.clear();
Ali
  • 839
  • 11
  • 21