PDA

View Full Version : how to sorting case insensitive


claudiof
04-04-2009, 03:43 PM
$myArray={"a","C","d"};

$myArray=sort($myArray);

print $myArray;

C
a
d

does it ready-to-use function can sort like this ?


a
C
d

Pyrokinesis
04-04-2009, 11:39 PM
I dont completely understand your question, but I think this is what you are after.

#python

test = ['a', 'A', 'B', 'b', 'd', 'D', 'c', 'C']
test.sort()
print test

['A', 'B', 'C', 'D', 'a', 'b', 'c', 'd']

claudiof
04-05-2009, 05:38 PM
yellow output example is clear enough I think.

standart sorting uses ascii sorting
your result is
['A', 'B', 'C', 'D', 'a', 'b', 'c', 'd']
but I want a case insensitive sort like :

['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd']

(in mel.
becasuse I am sure there is piles of libraries for python can do it)

Pyrokinesis
04-05-2009, 08:13 PM
You cn call python from within mel using the python command.

francisvega
04-06-2009, 10:45 AM
Hi!

Some usefull links:
http://www.sorting-algorithms.com/
http://www.codecodex.com/wiki/index.php?title=Bubble_sort#Maya_Embedded_Language
http://en.wikipedia.org/wiki/Sorting_algorithm

good luck!

greatPumpkin
04-06-2009, 05:49 PM
there is not a ready to use function for this, and as far as I know not a ready made function available in python either (though you can do it with two or three lines as an external call). Here's an ugly way to do it in mel:



$myArray={"a","C","d"};

// store an array of both lowercase and original case as 'n:x'
for($i = 0;$i < `size $myArray`;$i++)
{
$myArray[$i] = (`tolower $myArray[$i]` + ":" + $myArray[$i]);
}

// sort array based on lowercase characters
$myArray = sort($myArray);

// remove the temporary sorting aid by tokenizing around the ':'
for($i = 0;$i < `size $myArray`;$i++)
{
string $buffer[];
tokenize $myArray[$i] ":" $buffer;
$myArray[$i] = $buffer[1];
}



If you'd rather do it the python way do a search for 'case insensitive sort python' in google, there are a bunch of recipes

CGTalk Moderation
04-06-2009, 05:49 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.