views:

205

answers:

1

I have four arrays all containing related data,

Data Example:= (Array 1 | Array 2 | Array 3 | Array 4)

R.E.M.|In Time: The Best Of R.E.M. 1988-2003|album:6G5BGhEiLvck3kvKpSYw2y|Rock
Nick Drake|Family Tree|album:2euLAROPTmXDIDuU3qVMkf|Folk
Sonic Youth|Dirty|album:0QPkL6ap8riBoQ5xN8YDR3|Noise Rock

I would like to sort all arrays; filtered by one of the arrays, such as array 1: Artist. I'm programming the problem in AutoIt, but the answer could be in VBA/VBS/ASP, Java or C#

Can any body help -- it makes my head hurt.

=|)arkSprout=

+1  A: 

An object-oriented way of solving the problem is to define an Album type that contains all 4 pieces of information pertaining to an album, and have a single array of albums instead of 4 arrays.

Then, you can define comparison on albums as you wish. Here's an example in C# (would be very similar in Java):

class Program {
    static void Main(string[] args) {
        Album[] albums = { new Album {  artist = "potatotes", 
                                        family = "soup", 
                                        title = "a", 
                                        year=1546 },
                           new Album {  artist = "etc",
                                        family="blabla",
                                        title="blablabla",
                                        year = 1999 }
                         };
        Array.Sort(albums);
    }
}

class Album : IComparable<Album> {

    public int CompareTo(Album y) {
        return family.CompareTo(y.family);
    }


    public string title { get; set; }
    public string artist { get; set; }
    public int year { get; set; }
    public string family { get; set; }
}

Here, we have implemented the IComparable interface in terms of an Album's family. The general idea is to group all related information into a class, and then you can define comparison for objects of that class in terms of one of its fields.

As some might point out, there are more flexible ways to achieve this in C#, but I wanted to keep this as simple and language-agnostic as possible.

This should be possible to replicate as long as the language supports user-defined structures, but I'm not even sure AutoIT supports that. In that case, you'll have to keep your 4 arrays, and implement a custom sort function that sorts one of the arrays and replicates its swap operations on all 3 others at the same time. It's a lot more work though.

Dr_Asik
+1.But there is no such thing as "Object orientated" in AutoIt.
John
Collections are possible in AutoIt, but they're not very flexible:=[Code]$col = ObjCreate("Scripting.Dictionary")$col.Item("Artist") = "Sonic Youth"[/Code]But I can't see how to implement a Collection Sort, although changing the four arrays into an single n*4D array maybe the simpler route.Hummmm...=|)arkSprout=
DarkSprout
I don't think a single 4D array is of any use. If the language offers no appropriate support for custom structures, stick with 4 arrays. Implement a sort routine for 1 array, as you would do normally. Now, in the sort routine, each time you move elements, do the same on the other 3 arrays so they always stay in sync. When the routine completes, all 4 arrays are sorted according to one of them.Otherwise, see if you can switch to a better programming language...
Dr_Asik