Sorting the unsortable

You will have to forgive this blog entry as most of you will know this tip, but it was years since I last used it and when I wanted it last night it completely went out of my head, so I’m putting it up here as an aide memoire

So here is the problem, you have a java collection (in this case a List) containing java custom classes.

List<Address>

and here is the Address class that this is a List of:

package com.ldc.classes;
public class Address {
    private String firstLine;
    private String secondLine;
    private String postCode;
    private int ownerId;
    public String getFirstLine() {
        return firstLine;
    }
    public void setFirstLine(String firstLine) {
        this.firstLine = firstLine;
    }
    public String getSecondLine() {
        return secondLine;
    }
    public void setSecondLine(String secondLine) {
        this.secondLine = secondLine;
    }
    public String getPostCode() {
        return postCode;
    }
    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }
    public int getOwnerId() {
        return ownerId;
    }
    public void setOwnerId(int ownerId) {
        this.ownerId = ownerId;
    }
}

OK, You have got this List of Classes from something that the user has done, and you present them with this List, they then go “cant it be sorted by the Age of the person who lives there” , “errr well not really as the Address class does not contain the residents age, that is in a different class.

package com.ldc.classes;
public class Person {
    private int personId;
    private String firstName;
    private String lastName;
    private int age;  
}

“well just sort it out cant you”……..ffffssssss

Now I expect that most of you are thinking that it would be easier to do this with a SQL statement but believe me there are times when you cant or don’t want to do that, so we have to figure out a way or glue the 2 classes together and making them sortable, we are going to do that with a third class, and one that has a bit of magic in it called “implements Comparable”, our special class need to have the list of address in that we want to sort and something we want to sort them BY (in this case ownerID).

package com.ldc.classes;
public class SortableAddress implements Comparable<SortableAddress> {
    private Address address;
    private int ownerAge;
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public int getOwnerAge() {
        return ownerAge;
    }
    public void setOwnerAge(int ownerAge) {
        this.ownerAge = ownerAge;
    }
    public int compareTo(SortableAddress compareForm) {
        int compareAge = ((SortableAddress) compareForm).getOwnerAge(); 
        return this.ownerAge - compareAge;
    }
}

You can see that this class has 2 special bits in it, the first is the “implements Comparable” at the top and the second is the “CompareTo” at the bottom which I have tried to make as simple as possible so you can just swap out your values and get it working on your classes. what this means is you can now use, the standard Collection.sort that you would normally use to sort a list, but use it on your class, here is an example implementing that:

package com.ldc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.ldc.classes.Address;
import com.ldc.classes.SortableAddress;
public class SortMyClass {
    // OK we have a unsorted list of addresses and we are going to pass them to the function "getasortedListofAddress"
    public List<Address> getasortedListofAddress(
            List<Address> unsortedAddressList) {
        //first lets make a empty sortable address list (our special class)        
        List<SortableAddress> sortableAddressList = new ArrayList<SortableAddress>();
        //and loop though the unsorted list of addresses adding each on to the special class
        for (Address tempAddress : unsortedAddressList) {
            SortableAddress sortableAddress = new SortableAddress();
            sortableAddress.setAddress(tempAddress);
            //here we are getting the owners age which is the value we will be sorting by, to the special class
            sortableAddress.setOwnerAge(getTheOwnersAge(tempAddress
                    .getOwnerId()));
            sortableAddressList.add(sortableAddress);
        }
        //we now have a fully populated class that can be sorted using the standard Collections.sort
        Collections.sort(sortableAddressList);
        //hey presto we have sorted the list
        //now just run though the special class, to move the Address back into their old format, but now they are ordered!!!
        List<Address> listOfSortedAddressToReturn = new ArrayList<Address>();
        for (SortableAddress tempAddress : sortableAddressList) {
            listOfSortedAddressToReturn.add(tempAddress.getAddress());
        }
        return listOfSortedAddressToReturn;
    }
    private int getTheOwnersAge(int ownersID) {
        // some code that gets the owners age from its ID
        int ownersAge = 24;
        return ownersAge;
    }
}

give me a kick if you want me to explain it in more detail, but that that should be enough to provide a practical solution to most of you (and me when I next need it)

Old Comments
————
##### Mark(23/05/2012 14:43:57 GDT)
Oh, I agree generic would be best in real life, I was trying to do the easiest example to follow, but Dear readers Kerr is right when you actually put this bugger into practice
##### Kerr Rainey(23/05/2012 14:24:05 GDT)
I’d try to make this a little more generic by replacing int ownerAge (and associated getters/setters) with Comparable comparable.

Then you push down what it is that the SortableAddress is comparing. Since Integer is Comparable and we have auto boxing, this would be a drop in replacement for what you already have, bar the refactoring of the getter/setter names.

New Anime Series- Eureka Seven AO

First Episode Review for: Eureka Seven AO

Summary : Part of the ongoing Eureka Seven Mecha series, unsurprising an accident happens with a stolen Mecha near a young boy. (who somehow survives instead being brained by chunks of flying concrete)

Animation : Excellent quality throughout with good shading and attention to detail in the backgrounds, top mark especially on the electric effects (an often overlooked feature)

Plot Potential: This initially triggered my “oh no not again!” when the “accident” happened to the teenage kid, hopping they can do something fresh with it.

Characters: All stock so far, not a single original characters (although the sheer cowardice of the people who caused the accident was impressive), but given the obvious effort they have put into it I will give them time

Music: Faily strong dramatic music, heavy on the piano and sonic style effects.

Reminds me of: Eureka Seven

Overall: I’m not a huge Mecha fan, as they tend to struggle with new plot lines, but the visuals of this anime alone make it worth seeing.

Disclaimer: These are mini reviews of anime’s that are fresh out in Japan and are not licensed in the UK, buy them once they have been licensed or at the very least buy the merchandise, remember if the anime makers make a loss, THEY WILL STOP MAKING ANIME!!

New Anime Series- AKB0048

First Episode Review for: AKB0048

Summary : Set in a world where Entertainment is outlawed (work work you devils) and Idol concerts are run by high tech gorilla groups, a group of young fans aspire to be just like their heroes.

Animation : Good quality cell, mixed with very high quality computer and amazing quality backgrounds.

Plot Potential: If they get this right, then it will be a fab plot. its certainly quite original for an anime.

Characters: For what is basically an Idol anime, there is a lot of personal strife and angst which gives a sense of purpose over and above what is normal for this kind of series.

Music: Lots of nice idol tunes, lets see if they can maintain the standard.

Reminds me of: Sakura wars.

Overall: Normally Idol anime are background only, but this one held my attention and I will be watching to see how things progress, recommended.

Disclaimer: These are mini reviews of anime’s that are fresh out in Japan and are not licensed in the UK, buy them once they have been licensed or at the very least buy the merchandise, remember if the anime makers make a loss, THEY WILL STOP MAKING ANIME!!

New Anime Series- Acchi Kocchi

First Episode Review for: Acchi Kocchi

Summary : Day to day life at a school and one girls secret love for an oblivious boy (every one else knows about it though).

Animation : Really bright with strong colour blocks , backgrounds look a bit “stock”, but as its not an anime that in any way is meant to be relaistic , it’s nice and watchable

Plot Potential: Cutesy school love, not much plot

Characters: very one dimensional, completely flat, the script made out of stock phrase, feels a bit like Japanese stand up comedy.

Music: Bouncy poppy, but they lost the plot with the lyrics on the opening song

Reminds me of: Lucky Star but a bit more serious

Overall: This is a low stress background anime, not bad, but it wont go down in history for much.

Disclaimer: These are mini reviews of anime’s that are fresh out in Japan and are not licensed in the UK, buy them once they have been licensed or at the very least buy the merchandise, remember if the anime makers make a loss, THEY WILL STOP MAKING ANIME!!

New Anime Series- Accel World

First Episode Review for: Accel World

Summary : A social out caste at a school (mentally as well and physically) who’s only ability lies in the virtual world that is an every day part of school, is taken under the wing of a popular girl and introduced to “acceleration world” which lets him bring those virtual skills to the real world, but is there a dark side to this gift?

Animation : A strange style, sort of computer generated crayon, not sure it would go well in a lot of anime but with this subject matter it goes very very well

Plot Potential: Pulling the plot down to bare basics does not give me much hope, but it seems to have been perfectly implemented, which prenty of growth all round

Characters: The main character is a BIT tooo wimpy for my taste (needed a good slap), but the other characters were all done well, and despite only speaking about two words of Japanese the quality of the voice actors felt rich and vibrant.

Music: good general background music and a powerful end tune

Reminds me of: hmmm nothing springs to mind, lets say a mecha/tec version of yu yu hakusho

Overall: A excellent start to the new Anime season, and definitely on my repeat list

Disclaimer: These are mini reviews of anime’s that are fresh out in Japan and are not licensed in the UK, buy them once they have been licensed or at the very least buy the merchandise, remember if the anime makers make a loss, THEY WILL STOP MAKING ANIME!!