- Do or do not, there is no try. Always post all relevant code.
How To Ask Questions The Smart Way. 2004
Be precise and informative about your problemWe can't see your broken code.
- Did you put your variables in an array so that you could sort them? Arrays do not "only gives the value", they give which element you ask for.
- Or just test them.
double largest=DBL_MIN, second, third; if(A > largest){ third = second; second=largest; largest=A;} if(B > largest){ third = second; second=largest; largest=B;} ⋮
i have 8 variables with different values stored in them , and i am trying to get the variables in descending order , how can i get the name of the variables in descending order? :
largest variable
second largest variable
third largest variable
.
.
.
.
.
smallest variable
i tried the array sort but it only gives the value , but i am looking for a way to get the name of the variables that are holding these values
i know if i compare each variable individually then it can be done but i am looking for a simpler solution
Use an array of struct with two items: value and name. But you have to write your own sort routine.
i have 8 variables with different values stored in them , and i am trying to get the variables in descending order , how can i get the name of the variables in descending order? :
largest variable
second largest variable
third largest variable
.
.
.
.
.
smallest variable
i tried the array sort but it only gives the value , but i am looking for a way to get the name of the variables that are holding these values
i know if i compare each variable individually then it can be done but i am looking for a simpler solution
you could store them in array, and custom sort with 0 index holds the highest value
double _FirstArray[7] = { 1.23,1.34,1.33,1.45,1.77,1.11,1.98}; double _SortedFromMax[]; _SortArrayFromMax(_FirstArray,_SortedFromMax); ArrayPrint(_SortedFromMax); void _SortArrayFromMax(double &_original[],double &_modified[]) { int _size = ArraySize(_original); double _temp[]; ArrayResize(_temp,_size); ArrayCopy(_temp,_original,0,WHOLE_ARRAY); int _maxindex = 0; for (int i=0;i<_size;i++) { ArrayResize(_modified,i+1); _maxindex = ArrayMaximum(_temp); _modified[i] = _temp[_maxindex]; ArrayRemove(_temp,_maxindex,1); ArrayResize(_temp,_size-1); } ArrayFree(_temp); }
double indexmap[][2]; int rows=YourNumberOfVariables; ArrayResize(indexmap,rows); for(int i=0; i<rows; i++) { indexmap[i][0]=ValueOfVariable(i); indexmap[i][1]=i; } //--- sorting of indexmap[][] by first dimension (descending) ArraySort(indexmap,WHOLE_ARRAY,0,MODE_DESCEND); //--- indexmap is now sorted, descending, together with index, like so: [198.34, 5], [100.04, 2], ... for(int j=0; j<rows; j++) { double value=indexmap[j][0]; int i=(int)indexmap[j][1]; Print(i," ",value," ",NameOfVariable(i)); // print row i of the data }
thanks all but i dont want to just sort the values , i want to sort the values plus i want to get the name of the variable holding that value
if you want to use the variable name for later use calculation, you need to create a routine that will match the variable name (string) to prior variable that has identical name before the compilation but it is going to be complex, c++ does not support reflection
double a1 = 1.23; double a2 = 1.34; double a3 = 1.33; double a4 = 1.45; double a5 = 1.77; double a6 = 1.11; double a7 = 1.98; double _FirstArray[7]; ArrayFill(_FirstArray,0,1,a1); ArrayFill(_FirstArray,1,1,a2); ArrayFill(_FirstArray,2,1,a3); ArrayFill(_FirstArray,3,1,a4); ArrayFill(_FirstArray,4,1,a5); ArrayFill(_FirstArray,5,1,a6); ArrayFill(_FirstArray,6,1,a7); string _FirstArrayVarName[7] = { "a1","a2","a3","a4","a5","a6","a7" }; double _SortedFromMax[]; string _SortedFromMaxVarName[]; _SortArrayFromMax(_FirstArray,_SortedFromMax,_FirstArrayVarName,_SortedFromMaxVarName); Print("--------------original---------------"); ArrayPrint(_FirstArray); ArrayPrint(_FirstArrayVarName); Print("--------------reordered---------------"); ArrayPrint(_SortedFromMax); ArrayPrint(_SortedFromMaxVarName); void _SortArrayFromMax(double &_original[],double &_modified[],string &_orivarname[],string &_newvarname[]) { int _size = ArraySize(_original); if (_size!=ArraySize(_orivarname)) { Print("ERROR"); return; } double _temp[]; string _tempname[]; ArrayResize(_temp,_size); ArrayResize(_tempname,_size); ArrayCopy(_temp,_original,0,WHOLE_ARRAY); ArrayCopy(_tempname,_orivarname,0,WHOLE_ARRAY); int _maxindex = 0; for (int i=0;i<_size;i++) { ArrayResize(_modified,i+1); ArrayResize(_newvarname,i+1); _maxindex = ArrayMaximum(_temp); _modified[i] = _temp[_maxindex]; _newvarname[i] = _tempname[_maxindex]; ArrayRemove(_temp,_maxindex,1); ArrayResize(_temp,_size-1); ArrayRemove(_tempname,_maxindex,1); ArrayResize(_tempname,_size-1); } ArrayFree(_temp); ArrayFree(_tempname); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
i have 8 variables with different values stored in them , and i am trying to get the variables in descending order , how can i get the name of the variables in descending order? :
largest variable
second largest variable
third largest variable
.
.
.
.
.
smallest variable
i tried the array sort but it only gives the value , but i am looking for a way to get the name of the variables that are holding these values
i know if i compare each variable individually then it can be done but i am looking for a simpler solution