PDA

View Full Version : Sort MStringArray alphabetical


Buexe
05-07-2008, 09:35 PM
Hello earthlings! Is there a "smart" way to sort the contents of an MSelectionList or MStringArray into their alphabetical order. MEL has this nice "sort" command. Something similar in the API would be awesome! Any ideas?
Thanks in advance!

Buexe
05-07-2008, 09:52 PM
Now, this is what I came up with, sort of a workaround, isn`t it?

MStringArray stringArray;

stat = selList.getSelectionStrings( stringArray ); CE;

MString cmd = MString ("string $strings[]= { ");

unsigned int i= 0;

for ( i=0;i< stringArray.length(); i++)

{

cmd+= MString( "\"")+ stringArray[i] + "\"";

if ( i < stringArray.length()-1 )

cmd += ", ";

}

cmd += MString ( " }; sort $strings;" );

stringArray.clear();

MGlobal::executeCommand( cmd, stringArray, true, false );



MSelectionList sortedList;

for ( i=0;i< stringArray.length(); i++)

{

stat = sortedList.add ( stringArray[i] ); CE;

}

MItSelectionList itr ( sortedList );

theflash
05-13-2008, 06:54 PM
I have never done APIs before, but from what I understand in the work around what you did is that you created a Maya string and executed it as a MEL command. Right? I think it's a simple and neat work around. However I am just curious about general command execution from plugin. What would be faster, If you write a highly optimized sort algorithm in C++ (in your plugin itself) or the way you have done it by ussing MEL command itself?

Buexe
05-13-2008, 10:14 PM
To be honest: I dont know, which is faster. My guess would be that native c++ will usually be much faster. I use this particular piece of code in a custom command where it is highly unlikely that the user will input more than 100 items. So I guess for this context it is okay to use this highly questionable workaround.
I use MEL commands in other situations in the API too, like for setting keyframes. Because it is soooo much simpler to code. But dont tell anybody, okay? ; )
Cheers

Robert Bateman
05-14-2008, 10:37 AM
What would be faster, If you write a highly optimized sort algorithm in C++ (in your plugin itself) or the way you have done it by ussing MEL command itself?

The C++ algorithm, however in the global scheme of things the amount of time saved would be so minute as to be pointless having spent the effort. (unless of course you are processing thousands or strings, and doing it repeatedly, in which case it might have been worth it).

you could of course just use std::sort, if you provide a < operator for MString


#include <string.h>
bool operator<(const MString& a,const MString& b)
{
return strcmp(a.asChar(),b.asChar()) < 0;
}

#include <algorithm>
MStringArray gStringArray;

gStringArray.append( "sheep" );
gStringArray.append( "cat" );
gStringArray.append( "donkey" );
gStringArray.append( "cow" );
gStringArray.append( "horse" );

MString* start = &gStringArray[0];
MString* end = start + gStringArray.length();

std::sort(start,end);

for (unsigned i=0;i<gStringArray.length();++i)
{
std::cout << gStringArray[i].asChar() << std::endl;
}

Buexe
05-14-2008, 01:51 PM
Cool, thanks Robert! :beer:

CGTalk Moderation
05-14-2008, 01:51 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.