Sorting an array numerically - silly me

Author Topic: Sorting an array numerically - silly me  (Read 4284 times)

To sort an array numerically, I've tried using array objects. Guess what? Blockland doesn't have the required commands to sort them anyways.

So I resorted to a manual sort.

Code: [Select]
function updateLeaderboard() {
%leaderboard[0] = 0;
%temp = 1;
%i = 0;
%leaderboard[1] = 2;
%leaderboard[2] = 1;
%leaderboard[3] = 7;
%leaderboard[4] = 4;
%leaderboard[5] = 9;
%leaderboard[6] = 3;
echo(%leaderboard[1] SPC %leaderboard[2] SPC %leaderboard[3] SPC %leaderboard[4] SPC %leaderboard[5] SPC %leaderboard[6]);
for(%i = 1; %i < %leaderboard[0]; %i = %i + 1) {
for(%j = 1; %j < %leaderboard[0]; %j = %j + 1) {
if(%leaderboard[%i] > %leaderboard[%j]) {
%temp = %leaderboard[%j];
%leaderboard[%j] = %leaderboard[%i];
%leaderboard[%i] = %temp;
}
}
}
echo(%leaderboard[1] SPC %leaderboard[2] SPC %leaderboard[3] SPC %leaderboard[4] SPC %leaderboard[5] SPC %leaderboard[6]);
}

Now I'm pretty certain this should work but for some reason, it doesn't. I've been like 30 minutes trying to figure out why it doesn't work. Any help?
« Last Edit: February 19, 2017, 07:13:14 PM by Pastrey Crust »

Instead of making your own sorting function you could just create a guiTextListCtrl, add all the values in it, and use the sort or sortNumerical methods on it and grab the values after.

Just make sure you delete it when you've finished sorting.

The reason Blockland doesn't have them is because Torque arrays aren't really.. arrays. Just other ways of naming variables.

Thanks for your reply Crown but I just realized that %leaderboard[0] is 0 instead of 7. Changed that and it wotked. Silly me.

To sort an array numerically, I've tried using array objects. Guess what? Blockland doesn't have the required commands to sort them anyways.
Just for reference, they're not actually arrays.
==> %five = 5; %arr[%five] = 6; echo( %arr[5] == %arr5 );
1

While they can be used like arrays, they aren't actually. We have to define the size ourselves and they aren't even limited to just numbers.

In a technical sense, an array is an object with a list of values. What we have is several individual variables with their own singular values disguised as an array.
« Last Edit: February 19, 2017, 09:36:50 PM by Shift Kitty »

Ik, I noticed that with the Export command. As long as they can be operated as an array, they can be superman for all I care.

I would normally use quicksort because it's O(n log n) while the algorithm you're using is O(n2). But your sort input does not look large. How often do you need to sort?

Depends on the amount of saved data. The estimate would be 120 values to be sorted once every hour.

so doesn't matter too much, if it's already mostly sorted that could help too

But why the question? Is this an inefficient method?

you're using a bubble sort, which isn't terrible on small things, but doesn't scale well

but for your purposes it's fine
but I'd recommend changing a few things
Code: [Select]
for(%i = 2; %i < %leaderboard[0]; %i = %i + 1)
for(%j = 1; %j < %i; %j = %j + 1)
if(%leaderboard[%i] < %leaderboard[%j])
{
%temp = %leaderboard[%j];
%leaderboard[%j] = %leaderboard[%i];
%leaderboard[%i] = %temp;
}
do note that this will sort ascending order (0, 1, 2, 3)

if you want descending then the loops would need to go from the end to the start, instead of start to end

But why the question? Is this an inefficient method?

It's incredibly inefficient to implement any sorting algorithm in torkscript. If you have a lot of values just put them all in a gui text list ctrl and call sort there.

Long time ago I made this quick sort for records.
Code: [Select]
// Quick sort implementation
function QuickSortRecord(%list, %func, %left, %right)
{
// Standard function
if (!isFunction(%func))
%func = getMax;
// Avoid crash
%size = getRecordCount(%list);
%right = mClamp(%right, 0, %size - 1);
%left = mClamp(%left, 0, %size - 1);

// Nothing to compare
if (%right <= %left)
return %list;
// There could be a implementation of median-of-three,
// but it is hard due of how TorqueScript is built up.
%pivotIndex = mFloor((%left + %right) / 2);
%pivotNextIndex = %left;

// Partition
%pivotValue = getRecord(%list, %pivotIndex);
%list = swapRecord(%list, %pivotIndex, %right);

%storeIndex = %left;
for (%i = %left; %i < %right; %i++)
{
%a = Call(%func, getRecord(%list, %i), %pivotValue);
if (%a < 0)
{
%list = swapRecord(%list, %i, %pivotNextIndex);
%pivotNextIndex++;
}
}
%list = swapRecord(%list, %pivotNextIndex, %right);

// Continue on next
%list = QuickSortRecord(%list, %func, %left, %pivotNextIndex - 1);
%list = QuickSortRecord(%list, %func, %pivotNextIndex + 1, %right);
return %list;
}

// Swap data in record list
function swapRecord(%list, %first, %second)
{
// Avoid crash
%size = getRecordCount(%list);
%first = mClamp(%first, 0, %size - 1);
%second = mClamp(%second, 0, %size - 1);

%a = getRecord(%list, %first);
%b = getRecord(%list, %second);
%list = setRecord(%list, %first, %b);
%list = setRecord(%list, %second, %a);
return %list;
}

I think it worked last time I tested it. It could easily be changed to work with other types as well. However, it would be troublesome, but not impossible, to make it work with "arrays".

Code: [Select]
%list = 3 NL 2 NL 1;
%list = QuickSortRecord(%list, getMax, 0, 3);

That's completely unnecessary. Sorting a gui text list ctrl already uses quicksort.

Mind if I ask how that's more efficient than the method I posted since it has noticeably more lines?

That's completely unnecessary. Sorting a gui text list ctrl already uses quicksort.

True, but can you create GUI objects on a dedicated server?

Mind if I ask how that's more efficient than the method I posted since it has noticeably more lines?

It's all about complexity. Having an algorithm that is bigger does not mean that it's slower. In fact, your does n2 operations while mine on average does n*log(n). It's considerably faster. But, if you have few values like that, then your one works fine.