0

I have already searched through the site, finding many resources, none of which answer this question. Consider reading before down voting or marking as duplicate.

I CANNOT USE A COMPARATOR INTERFACE OR .sort()

I have an ArrayList of Photo objects named "photoAlbum" like this ArrayList<Photo> photoAlbum = new ArrayList<Photo>(); where each Photo object contains a String attribute named photographerName. I need to to sort the Photo objects alphabetically with the photographerName attribute. I understand that I can use bubble sort or exchange sort, but how can I actually sort the Photo objects themselves inside the photoAlbum ArrayList based off of each photos photographerName attribute?

Just tips or generic example would be unbelievably appreciated.

PS I am not allowed to use a Comparator interface or the .sort method.

Chizx
  • 351
  • 2
  • 5
  • 16
  • 1
    How hard did you try to search for duplicates yourself? I believe this is a duplicate of http://stackoverflow.com/questions/1206073/sorting-a-collection-of-objects, and yes, I did read both your question and that one. – Dawood ibn Kareem Mar 07 '14 at 04:38
  • I am not allowed to implement a comparator interface. – Chizx Mar 07 '14 at 04:40
  • Not your fault, but I despair of professors who set an assignment, then forbid the students from doing the assignment the correct way. – Dawood ibn Kareem Mar 07 '14 at 04:42
  • They are wanting us to understand the fundamentals of using the sorting algorithms. I agree though.. Why does this always happen to me.. please remove the duplicate mark so that people actually view my question. Please. – Chizx Mar 07 '14 at 04:44
  • I guess that's the difference between learning computer programming and learning computer science. I got my first programming job in 1990, and I don't understand the fundamentals of the sorting algorithms. Every language you're ever likely to program in has some equivalent to `Collections.sort` and it's only in a very limited set of applications that it's wrong to just use it. – Dawood ibn Kareem Mar 07 '14 at 04:49

3 Answers3

1

try this :

Collections.sort(your array list object, new comparator());

public class CustomComparator implements Comparator<Your Array List> {
        @Override
        public int compare(ClassObj o1, ClassObj o2) {
            return String.valueOf(o1.getName()).compareTo(String.valueOf(o2.getName()));
        }
    }

or try this logic :

  import java.util.*;

    public class BubbleSort {

   public static void main(String[] args) {
    String l[]={"ABCD" , "XYZ" , "DEF" , "PQR"};
    BubbleSort(l);
    for(int i=0; i<l.length; i++)
    {
        System.out.println(l[i]);
    }
  }

 private static void BubbleSort(String[] array) {
    String t;
    for(int i=0; i<array.length; i++) {
        for(int j=0; j<array.length-1-i; j++) {
        if(array[j].compareTo(array[j+1])>0) {
            t= array[j];
            array[j] = array[j+1];
            array[j+1] = t;
        }
    }
    }
}
   }
Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
1

You could create a method

public static comparePhotos(Photo a, Photo b) {
    return a.photographerName.compareTo(b.photographerName);
}

Then anywhere in your sorting algorithm where you want to compare Photos:

instead of doing this:

if(photo1 < photo2) //this wouldn't work

you do:

if(comparePhotos(photo1, photo2) < 1)

comparePhotos will return a negative if photo1.photographerName is less than photo2.photographerName, 0 if they're equal, and a positive if photo1.photographerName is greater than photo2.photographerName

Tyler
  • 17,669
  • 10
  • 51
  • 89
0

There are two possibilities:

  1. Photo class will implement Comparable interface and you will implement compareTo method in Photo class. Then you just need to call Collections.sort(photoAlbum);

  2. You will implement your own Comparator and call Collections.sort(photoAlbum, comparator);. In this way, you don't need to modify Photo class.

Edit: If you can't use this, just implement your favorite sort algorithm. Or have a look into Java implementation of sort function.

Robert Balent
  • 1,452
  • 11
  • 21