Jump to content
slacky

Sorting Array of TIA..

Recommended Posts

I wish to be able to sort an array of TIntArray by the first "sub-columen" (first index in each subarray) where the int the resulting order is Hi-Low/Low-Hi.

 

EG:

 ATIA := [[10,0,1], [43,1,1], [13,5,7]];

 ATIA := SomeSortingAlgo(ATIA, index:=0);
 ATIA := [[43,1,1], [13,5,7], [10,0,1]];

Is there any simple way to do this in SCAR, or do would I need to write an algorithm for sorting the ATIA by first index in each nested array my self?

 

Also, just while i'm here, I would like to know why I cant simply do this:

var
 ATIA: T2DIntArray; 
begin
 ATIA := [[10,0,1], [43,1,1], [13,5,7]];
end.

//I gotta do this..:
var
 ATIA: T2DIntArray; 
begin
 SetLength(ATIA,3)
 ATIA[0] := [10,0,1];
 ATIA[1] := [43,1,1];
 ATIA[2] := [13,5,7]; 
end.

 

 

//Edit:

- The sorting part is solved. Modified an algo that Freddy posted last year to fit my use.

Yet, I wish to know if there's a function built in for this.. Also, question two stands unsolved.

Edited by slacky
Link to comment
Share on other sites

http://wiki.scar-divi.com/index.php?title=SortATPAByFirst

http://wiki.scar-divi.com/index.php?title=SortTIAEx

 

[sCAR]procedure SortATIAByFirst(var ATIA: T2DIntArray);

var

I, H: LongInt;

begin

H := High(ATIA);

for I := 0 to H do

SortTIAEx(ATIA, ATIA[0]);

end;[/sCAR]

 

Considered adding this to OSI at one point but honestly I can't see any real uses for it. What are you using it for?

Link to comment
Share on other sites

I was really just playin' around, learning more about SCAR+Pascal-syntax..

Did some speed comparisons (for my use) of pascal script vs python to se wether or not I would need to create C++ (or delphi) plugins (for speed optimalization).

 

I offen use such function to categorize pixels (or pixel areas) by their similarity to some other pixels.

A dirty example (would be best in grayscale...):

>> Differance between pixel one RGB, and RGB2 [abs(R-R2...G-G2), x,y]... contunue the loop for all pixels..

>> When the loop is done we would sort the array of TIntArray by lowest value first..

>> Value[0] will then contain the best match and a set of coordinats. If they dont fit the bill use next in line...

 

There are many other uses, but I like that example...

I've used such a function very offen in different causes (in python). Mainly (the first index) is used as a "value-placeholder" in comparison algorithms where I need to get all results (x,y), and also ordered (HiLow/LowHi) by there given value (first index in the subarray).

 

As to your answer: I believe you missunderstod what I was tryin' to achive... I will answer my own question with this:

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Quckly print an ATIA (for debugging)...
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}
function ATIAToStr(ATIA: T2DIntArray): String;
var
 Data: String; 
 i: Integer;
begin
 Data := '';
 for i:=0 to High(ATIA) do 
   Data := Data + '['+ TIAToStr(ATIA[i]) + '] ';
 SetLength(ATIA, 0);    
 Result := Data;
end;

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Sort an Array of IntegerArray by the given index-id
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}
procedure SortATIAByIndex(var Arr: Array of TIntArray; index:Integer);
var
 CurIndex, TmpIndex, Max: Integer;
begin
 Max := High(Arr);
 for CurIndex := 1 to Max do
   for TmpIndex := CurIndex downto 1 do begin
     if not (Arr[TmpIndex][index] < Arr[TmpIndex - 1][index]) then
       Break;
     Swap(Arr[TmpIndex - 1], Arr[TmpIndex]);
   end;
end;


//Test me...
var
 ATIA: T2DIntArray; 
begin
 SetLength(ATIA, 5)
 ATIA[0] := [64, 0,1];
 ATIA[1] := [43, 1,12];
 ATIA[2] := [23, 4,2];  
 ATIA[3] := [27, 4,2];
 ATIA[4] := [34, 2,8]; 

 WriteLn(ATIAToStr(ATIA));  

 //Sort the array by index[x][0] in each nested array.
 SortATIAByIndex(ATIA, 0);

 WriteLn(ATIAToStr(ATIA));  
end.

- This sloved it.

Edited by slacky
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...