If I have a collection of strings such as a StringArray, whats the most efficent way of sorting them alphabetically?
I'm used to having some inbuilt sort method although dont think thats an option in Jade 6.3?
Thanks,
Tim
Code: Select all
bubbleSort(pB_CaseSensitive : Boolean; pB_Ascending : Boolean) lockReceiver, updating;
vars
iSortedSoFar, iCurrentIndex, iAscDescComparator : Integer;
sCurrent, iNext : String;
begin
if size > 1 then
if pB_Ascending then
iAscDescComparator := GreaterThan;
else
iAscDescComparator := LessThan;
endif;
foreach iSortedSoFar in 1 to size - 1 do
foreach iCurrentIndex in 1 to size-iSortedSoFar do
sCurrent := self[iCurrentIndex];
iNext := self[iCurrentIndex + 1];
if sCurrent.compareTo(iNext, pB_CaseSensitive) = iAscDescComparator then
self[iCurrentIndex] := iNext;
self[iCurrentIndex + 1] := sCurrent;
endif;
endforeach;
endforeach;
endif;
end;
Code: Select all
compareTo(pS_Comparer : String; pB_CaseSensitive : Boolean) : Integer;
vars
iIndex, iCurrent : Integer;
begin
if (pB_CaseSensitive and self = pS_Comparer) or (not pB_CaseSensitive and self.toLower = pS_Comparer.toLower) then
return EqualTo;
endif;
foreach iIndex in 1 to length.min(pS_Comparer.length) do
iCurrent := self[iIndex:1].Character.compareTo(pS_Comparer[iIndex:1].Character, pB_CaseSensitive);
if iCurrent <> EqualTo then
break;
endif;
endforeach;
if iCurrent = EqualTo then
if length > pS_Comparer.length then
return GreaterThan;
elseif length < pS_Comparer.length then
return LessThan;
else
return EqualTo;
endif;
else
return iCurrent;
endif;
end;
Code: Select all
compareTo(pC_Comparer : Character; pB_CaseSensitive : Boolean) : Integer;
vars
iSelf, iComparer : Integer;
begin
if pB_CaseSensitive then
iSelf := self.Integer;
iComparer := pC_Comparer.Integer;
else
iSelf := self.toLower.Integer;
iComparer := pC_Comparer.toLower.Integer;
endif;
if self.Integer > pC_Comparer.Integer then
return GreaterThan;
elseif self.Integer < pC_Comparer.Integer then
return LessThan;
else
return EqualTo;
endif;
end;
Code: Select all
dyna.addExternalKey(TimeStampInterval,8,false,false); // age
dyna.addExternalKey(TimeStampInterval,8,false,false);
foreach emp in root.allEmployeesByName do
dyna.putAtKey(emp.getAge, emp.getLengthOfService, emp);
endforeach;
Code: Select all
putAtKey(keys: KeyType;value: MemberType) updating;
Code: Select all
Method called with an incorrect number of parameters- code: 1414
Code: Select all
dyna.endKeys( allowDuplicates );
Code: Select all
vars
dd : DynaDictionary;
obj : Object; // arbitrary object
iter : Iterator;
i : Integer;
str : String;
begin
create dd transient;
dd.setMembership ( Object ); // arbitrary class
dd.addExternalKey( String, 8, false, false ); // define collection
dd.addExternalKey( Integer, 8, false, false );
dd.endKeys( false );
dd.putAtKey( "cat", 2, app ); // load up some random data
dd.putAtKey( "dog", 2, app );
dd.putAtKey( "cat", 1, app );
dd.putAtKey( "cat", 3, app );
dd.putAtKey( "ant", 2, app );
iter := dd.createIterator; // list key values in order
while iter.next( obj ) do
iter.getCurrentKeys( str, i );
write str & Tab & i.String;
endwhile;
epilog
delete dd;
delete iter;
end;
Code: Select all
create dynaDict transient; //DynaDitionary
create keysArray transient; //StringArray
dynaDict.setMembership(M2SmRequestItem);
if sortOptions.key1 <> 0 then
if sortOptions.key1 = 1 then dynaDict.addExternalKey(Integer,10,false,false); else
dynaDict.addExternalKey(Integer,10,true,false); endif;
endif;
if sortOptions.key2 <> 0 then
if sortOptions.key2 = 1 then dynaDict.addExternalKey(TimeStamp,8,false,false); else
dynaDict.addExternalKey(TimeStamp,8,true,false); endif;
endif;
if sortOptions.key3 <> 0 then
if sortOptions.key3 = 1 then dynaDict.addExternalKey(String,150,false,false); else
dynaDict.addExternalKey(String,150,true,false); endif;
endif;
if sortOptions.key4 <> 0 then
if sortOptions.key4 = 1 then dynaDict.addExternalKey(Integer,4,false,false); else
dynaDict.addExternalKey(Integer,4,true,false); endif;
endif;
if sortOptions.key5 <> 0 then
if sortOptions.key5 = 1 then dynaDict.addExternalKey(String,150,false,false); else
dynaDict.addExternalKey(String,150,true,false); endif;
endif;
if sortOptions.key6 <> 0 then
if sortOptions.key6 = 1 then dynaDict.addExternalKey(String,150,false,false); else
dynaDict.addExternalKey(String,150,true,false); endif;
endif;
if sortOptions.key7 <> 0 then
if sortOptions.key7 = 1 then dynaDict.addExternalKey(Integer,4,false,false); else
dynaDict.addExternalKey(Integer,4,true,false); endif;
endif;
dynaDict.endKeys(true);
foreach req in pList.M2SmRequestItemDictionary do
keysArray.purge;
keysArray.add(null);keysArray.add(null);keysArray.add(null);
keysArray.add(null);keysArray.add(null);keysArray.add(null);
keysArray.add(null);keysArray.add(null);keysArray.add(null);
if sortOptions.key1 <> 0 then
keysArray.insert(1,req.uid1.String);
endif;
if sortOptions.key2 <> 0 then
keysArray.insert(2,req.created.String);
endif;
if sortOptions.key3 <> 0 then
if req.EbdAccessCardRI.myRequestItemType <> null then
keysArray.insert(3,req.EbdAccessCardRI.myRequestItemType.name);
endif;
endif;
if sortOptions.key4 <> 0 then
if req.EbdAccessCardRI.cardAmount <> null then
keysArray.insert(4,req.EbdAccessCardRI.cardAmount.String);
endif;
endif;
if sortOptions.key5 <> 0 then
if req.EbdAccessCardRI.myOrganisation <> null then
keysArray.insert(5,req.EbdAccessCardRI.myOrganisation.name);
endif;
endif;
if sortOptions.key6 <> 0 then
if req.EbdAccessCardRI.myOrganisation <> null then
keysArray.insert(6,req.EbdAccessCardRI.myOrganisation.name);
endif;
endif;
if sortOptions.key7 <> 0 then
if req.myPriority <> null then
keysArray.insert(7,helper_reqFlagInt(req).String);
else
keysArray.insert(7,'6');
endif;
endif;
if sortOptions.key8 <> 0 then
keysArray.insert(8,null);
endif;
dynaDict.putAtKey(keysArray[1].Integer,keysArray[2].TimeStamp,keysArray[3],keysArray[4].Integer,keysArray[5],keysArray[6],keysArray[7].Integer,req);
endforeach;
iter := dynaDict.createIterator;
Code: Select all
if ExternalKey.size = 5 then
dynaDict.putAtKey(keysArray[1]....
else if ExternalKey.size = 4 then
dynaDict.putAtKey(keysArray[1]....
else if ExternalKey.size = 3 then
dynaDict.putAtKey(keysArray[1]....
else if ExternalKey.size = 2 then
dynaDict.putAtKey(keysArray[1]....
etc
I tried this just now which is almost perfect although it breaks on timestamps, as it sorts them as a string value and 01/01/2012 is lower then 10/01/1999I'd go with 7 String keys on the ExtKeyDynaDict, and then if they've only chosen 3 of 7 sort options, set the remaining key values to a null string as you don't care about the sort ordering from that point onwards.
Then, when you're deciding what to use for each of the 7 string key parameters to the putAtKey, you can "generate" a string key from the underlying data in a format that will sort correctly regardless of the type of data that is actually in that column.
Cheers,
BeeJay.
Return to “General Discussion”
Users browsing this forum: No registered users and 26 guests