Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split out a variable, sort alpha, then re-join. 2

Status
Not open for further replies.

hallux

Programmer
Feb 25, 2003
133
US
Hi everybody,
I am modifying a formula that has one or more names added to a variable with a "/" as delimeter. It needs to have all of the names sorted in alpha, I would like to split out the names, sort them and then join them back together again.

StringVar EmpNames;
// EmpNames contains "Smith, J/Jones B/Brown M"
// Next split out into array
split(EmpNames, "/") // results in array with 3 names
sort array // not sure what to do here
join array // or here
StringVar EmpNamesOrdered
EmpNamesOrdered := array

As you can see I have the logic in place, but need some help with the array.

CR 8.5 Dev
 
Here ya go.



StringVar EmpNames:="Smith, J/Jones B/Brown M";
stringvar array sort;
numbervar loop;
numbervar loop2;
stringvar temp;

sort:=split(EmpNames, "/");


for loop2:=1 to ubound(sort)-1 do(
for loop:=1 to ubound(sort)-1 do(
if sort[loop]>sort[loop+1] then

(temp:=sort[loop];
sort[loop]:=sort[loop+1];
sort[loop+1]:=temp)));



StringVar EmpNamesOrdered:=join(sort,"/")

Mike
 
ok...let's take a stab at this

@reorder_Names

WhilePrintingRecords;
StringVar EmpNames;
StringVar Array Temp := split(EmpNames, "/");
StringVar SortedNames := "";
NumberVar iCount;
NumberVar ncount;
StringVar Hold;

//check to see if there is more than one array element
if UBound(Temp) < 2 then
SortedNames := EmpNames
else
(
//simple sort routine
for iCount := 1 to ubound(Temp)-1 Do
(
for nCount := iCount + 1 to ubound(Temp) Do
(
if Temp[iCount] > Temp[nCount] then
(
Hold := Temp[iCount];
Temp[iCount] := Temp[nCount];
Temp[nCount] := Hold;
);
);
);

// now Join the elements into a single string
SortedNames := Join(Temp,&quot;\&quot;)
);

SortedNames;







Jim Broadbent
 
Wow!
Thanks alot mbarron and ngolem. I tried out mbarron's and it works perfectly. I'll check out ngolem's next.

-Brent
 
Hi everyone.

Great to see the traditional ripple sort still being useful. But you are processing too many unnecceaasry loops...
MBarrons second loop can be rewritten...
for loop:=1 to ubound(sort)-loop2 do(

The first time through loop2 will put the largest value into the final postion, so you no longer need to check it. The second iteration will load the second highest value.

Editor and Publisher of Crystal Clear
 
In my case I do reduce the number of comparisons with each pass.

Actually I don't think a real efficient sort routine is required since the number of items to be sorted is very small making more complex sorts actually less efficient... at least that is my understanding

Jim Broadbent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top