A simple example of a 2 dimensional array,initialized(not sure how to) and incorporating 3 or more numbers and labels would be a huge help...
I can't seem to find an example of what you described anywhere...(wiki, internet etc)
Sorry, I was too quick to respond. A 2 dimensional array can only be of one type: either string for the symbols/sector names or numbers for the percentages. My fault was assuming that an array could hold two different types (the name and the percentage). Instead, that behaviour is possible with dictionaries (which PowerLangauge doesn't have).
Both that's not a problem, it just takes more code. One of the ways to approach this is the following:
* Create a two-dimensional (2D) array, just like you'd have in Excel with the first column holding the percentages and the second column an unique identifier. For example:
Code: Select all
Column A | Column B
23.39 | 1
34.08 | 2
-10.00 | 3
* We then need to sort the 2D array based on the first column (the percentages) from high to low, just like in Excel with the A-Z button. Since the second column is associated with the first, it moves along with the sorting:
Code: Select all
Column A | Column B
34.08 | 2
23.39 | 1
-10.00 | 3
* The last step is then to use the identifier (in the example above: 2, 1, 3) and connect them with their name so that we end up with this:
Code: Select all
Column A | Column B | Column C
34.08 | 2 | Retail
23.39 | 1 | Financial services
-10.00 | 3 | Construction
In the final output we drop the 'Column B' with the identifier since we don't need that anymore. In that case we end up with:
Code: Select all
Column A | Column C
34.08 | Retail
23.39 | Financial services
-10.00 | Construction
And then we have sorted the percentage and associated it with its name. Now onward to the code:
Code: Select all
Variables:
x(0);
Arrays:
Sectors[10](""),
Returns[10, 2](0);
once begin
ClearDebug;
// We'll pull the sector name from this array later on
Sectors[0] = "ES";
Sectors[1] = "Tech";
Sectors[2] = "Fin";
Sectors[3] = "Banks";
Sectors[4] = "Automobile";
Sectors[5] = "Retail";
Sectors[6] = "Real estate";
Sectors[7] = "Medicine";
Sectors[8] = "SomethingElse";
Sectors[9] = "AnotherThing";
// Output
Print("These are the names we match the numbers later with:");
for x = 0 to 9 begin
Print(NumToStr(x, 0), " - Name: ", Sectors[x]);
end;
// We use the second index of the second array as our 'identifier'
Returns[0, 1] = 1;
Returns[1, 1] = 2;
Returns[2, 1] = 3;
Returns[3, 1] = 4;
Returns[4, 1] = 5;
Returns[5, 1] = 6;
Returns[6, 1] = 7;
Returns[7, 1] = 8;
Returns[8, 1] = 9;
Returns[9, 1] = 10;
// The first index of the second array will be our 'percentage returns'
Returns[0, 0] = 234.49;
Returns[1, 0] = 4.49;
Returns[2, 0] = -30.3;
Returns[3, 0] = -99;
Returns[4, 0] = 0.01;
Returns[5, 0] = 120;
Returns[6, 0] = 0.003;
Returns[7, 0] = 564;
Returns[8, 0] = 5;
Returns[9, 0] = 25;
// Output before sorting
Print(NewLine, NewLine, "Before sorting");
Print("(these are the percentages and their unique identifier):");
for x = 0 to 9 begin
Print("Identifier: ", NumToStr(Returns[x, 1], 0), " - ",
"Percentage: ", NumToStr(Returns[x, 0], 5));
end;
// Now sort the two-dimensional array
Sort2DArrayByKey(Returns, 10, 1);
// Output after sorting
Print(NewLine, NewLine, "After sorting:");
Print("(now we've sorted based on the percentages)");
for x = 0 to 9 begin
Print("Identifier: ", NumToStr(Returns[x, 1], 0), " - ",
"Percentage: ", NumToStr(Returns[x, 0], 5));
end;
// Now we need to match the identifiers with the sector/instrument name
Print(NewLine, NewLine, "Final output:");
Print("(matching the identifiers of the 2D array with the name from the 'Sectors' array)");
for x = 0 to 9 begin
Print("Identifier: ", NumToStr(Returns[x, 1], 0), " - ",
"Name: ", Sectors[Returns[x, 1]], " - ",
"Percentage: ", NumToStr(Returns[x, 0], 2), "%");
end;
end;
This code generates the following output:
Code: Select all
These are the names we match the numbers later with:
0 - Name: ES
1 - Name: Tech
2 - Name: Fin
3 - Name: Banks
4 - Name: Automobile
5 - Name: Retail
6 - Name: Real estate
7 - Name: Medicine
8 - Name: SomethingElse
9 - Name: AnotherThing
Before sorting
(these are the percentages and their unique identifier):
Identifier: 1 - Percentage: 234.49000
Identifier: 2 - Percentage: 4.49000
Identifier: 3 - Percentage: -30.30000
Identifier: 4 - Percentage: -99.00000
Identifier: 5 - Percentage: 0.01000
Identifier: 6 - Percentage: 120.00000
Identifier: 7 - Percentage: 0.00300
Identifier: 8 - Percentage: 564.00000
Identifier: 9 - Percentage: 5.00000
Identifier: 10 - Percentage: 25.00000
After sorting:
(now we've sorted based on the percentages)
Identifier: 8 - Percentage: 564.00000
Identifier: 1 - Percentage: 234.49000
Identifier: 6 - Percentage: 120.00000
Identifier: 10 - Percentage: 25.00000
Identifier: 9 - Percentage: 5.00000
Identifier: 2 - Percentage: 4.49000
Identifier: 5 - Percentage: 0.01000
Identifier: 7 - Percentage: 0.00300
Identifier: 3 - Percentage: -30.30000
Identifier: 4 - Percentage: -99.00000
Final output:
(matching the identifiers of the 2D array with the name from the 'Sectors' array)
Identifier: 8 - Name: SomethingElse - Percentage: 564.00%
Identifier: 1 - Name: Tech - Percentage: 234.49%
Identifier: 6 - Name: Real estate - Percentage: 120.00%
Identifier: 10 - Name: - Percentage: 25.00%
Identifier: 9 - Name: AnotherThing - Percentage: 5.00%
Identifier: 2 - Name: Fin - Percentage: 4.49%
Identifier: 5 - Name: Retail - Percentage: 0.01%
Identifier: 7 - Name: Medicine - Percentage: 0.00%
Identifier: 3 - Name: Banks - Percentage: -30.30%
Identifier: 4 - Name: Automobile - Percentage: -99.00%
As the final output shows, the numbers are sorted from high to low, and the correct name is displayed with each number. I hope this is what you meant?