sorting

I have been trying to come up with a way to sort an array while still maintaining a link with it’s counterpart.

I’m pulling in a tab delineated file from my database and populating several arrays (title, genre, artist, etc), the problem comes when I want to sort these arrays.

How do I sort the genre array while still maintaining a link to the title array?

I have looked into hashtables, but I run into a duplicate key issue because the genre is a short list and several titles will have the same name.

This is the problem Raoul had here : http://forum.unity3d.com/viewtopic.php?t=9965&view=previous&sid=9a61dc25b8e716427d8fe10e3b513cfd

I can iterate through the list and compare them to a predefined list (like genre) using a for loop and a switch statement - but that won’t work for artist category where I don’t have the list of artists up front.

thanks

Flash

not 100% sure what exactly you want to do, recreate a relational DB sort of thing? Have you looked into using SQLite within Unity?

http://forum.unity3d.com/viewtopic.php?t=8432

Or will ArrayList.Sort() do what you want?

Cheers.

So, you want to take something like this:

[0, Pear, fruit]
[1, Apple, fruit]
[2, Steak, meat]
[3, Beer, drink]

and be able to sort the whole thing one of the items, so if we chose the second column:

[1, Apple, fruit]
[3, Beer, drink]
[0, Pear, fruit]
[2, Steak, meat]

???

Maybe you could use the C# build in SortedList class and just write your own Comparer to be used when the SortedList is instantiated.

Charles - that is exactly what I’m trying to do.

I am building a web player so if it’s not in the default player, I don’t want to import it. (trying to keep my overhead as low as possible)

how would I do what you have described? sorting the second row?

EDIT:
I googled SortedList and found this post that looks like it may help http://forum.unity3d.com/viewtopic.php?p=26444&highlight=&sid=b372630a514f89c16f141cf891971a42

Freyr is talking about using Icomparable and compareTo, to sort the list.

right now the arrays that I have built are separate, so I think my first step is going to be to create an array like Charles has in the simple example above

[title, genre, artist, etc]

then write a script like Freyr has that sorts the second column while dragging all of the rest of the components with it.

any other suggestions?

Thanks

Flash

I’ve not done this in Unity, and I’m a .NET noob, but .NET can sort one array based on the values of another array of equal length. The key value in the key array at a given index is used to look up the value in the value array at the same index. You might want to Google something like “.NET key-value sorting”. :?

I would think that this could also be a job for IComparable.

Well I’m not all that happy with this code, but it does work (more or less)

Here’s what I was able to come up with, maybe somebody out there will be able to use this or improve upon it.

This is an adaption from Raoul’s post here:
http://forum.unity3d.com/viewtopic.php?t=9965&view=previous&sid=9a61dc25b8e716427d8fe10e3b513cfd

function sortHashtable (inpHash : Hashtable) : Array {
	var q : int; //this is used to ensure no duplicate keys are made 
	var arrayValues = new Array(); //intermediate array used to sort the data
	var valuesKeysHash = new Hashtable(); //new hashTable for the new order of values
	var returnArray = new Array(); //what gets returned
	// create arrays of values and reverse keys and values in valuesKeysHash hashtable
	for (key in inpHash.Keys) {
		var checkDuplicate : boolean = noDuplicates(arrayValues,inpHash,key); //this will check for duplicate keys
		if (checkDuplicate == false)
			arrayValues[arrayValues.length] = inpHash[key]; //no duplicate
		else {
			arrayValues[arrayValues.length] = inpHash[key] + q; //if duplicate - then add q to the end to make it unique ***can you say hack job?***
			q++;
		}
		valuesKeysHash.Add(arrayValues[arrayValues.length-1], key); //create a new hash with the values as keys and keys as values
	}
	arrayValues.Sort();// sort the array with values
	for (iVal in arrayValues)   // create new array holding the new order of keys and return it
		returnArray[returnArray.length] = valuesKeysHash[iVal]; //populate returnArray with the keys in desired order
	return returnArray; //return this value
} 

//This function checks for duplicate, returns true if it is
function noDuplicates(arrayValues : Array, inpHash : Hashtable, key : String) : boolean {
	for (k = 0 ; k < arrayValues.length ; k++ ) {
		if (arrayValues[k] == inpHash[key])
			return true;
	}
	return false;
}

I’ve solved his duplicate key entry by adding a number to the end of the String. I need to add one more function that takes the subcategories and alphabetizes them, so that when there is a duplicate key, I get that section of the list in a-z order vice some random one that I have now.

Hope this helps someone out - and if you can improve upon this code please feel free

Flash