| Array sort [message #915214] |
Sun, 16 September 2012 16:52  |
Dan Darnell Messages: 145 Registered: November 2011 Location: Arkansas |
Senior Member |
|
|
I am trying to figure out how array sorting is implemented in EDT. I found the sort() function in the List implementation but it takes a user-defined sort function as a parameter. I am familiar with this from the RUILib.sort() implementation in EGL CE but that function would accept null as a parameter to use a default alpha sort. Is the intention in EDT that a developer MUST provide his/her own sort function implementation or are there default implementation(s) tucked away somewhere?
I know in JavaScript that the sort() function accepts a user-defined sort function as a parameter or you can omit the function definition and get an alpha sort. That concept would be nice to have in EDT. (As I say, this is the way it was implemented in RUILib in EGL CE.)
FYI, I'm using the following as sort function implementations of my own devising...is there a better way?
function sortFunctionAlpha (a any in, b any in) returns(int)
aString string = a as string;
bString string = b as string;
if (aString < bString)
return -1;
end
if (aString > bString)
return 1;
end
return 0;
end
function sortFunctionInteger (a any in, b any in) returns(int)
return (a as int - b as int);
end
Thanks,
Dan
|
|
|
|
|
| Re: Array sort [message #1006846 is a reply to message #915615] |
Sat, 02 February 2013 14:53  |
Richard Moulton Messages: 87 Registered: August 2011 Location: Devon, UK |
Member |
|
|
I really like this. I was wondering how to deal with array sorting and found this post and then I was wondering how to deal with sorting an array of records, sorting on multiple elements in my array and this worked really well.
// Portfolio transactions are sorted by transaction type within date; the
// earliest BUY transaction would be first
function sortTransactions(a any in, b any in) returns(int)
aTransaction portfolioRecord = a as portfolioRecord;
bTransaction portfolioRecord = b as portfolioRecord;
case
when ( aTransaction.trndate > bTransaction.trndate )
return 1;
when ( aTransaction.trndate < bTransaction.trndate )
return -1;
otherwise
case
when ( aTransaction.trntype > bTransaction.trntype )
return 1;
when ( aTransaction.trntype < bTransaction.trntype )
return -1;
otherwise
return 0;
end
end
end
This successfully sorted my array of transactions by transaction type within transaction date. I was pleasantly surprised how easy this was.
Richard
|
|
|
Powered by
FUDForum. Page generated in 0.01532 seconds