Author Topic: Sorting numbers  (Read 6903 times)

TheThingIs

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 278
    • HCS Voicepacks
Sorting numbers
« on: July 25, 2016, 07:17:55 PM »
What would be the best way in VA to sort 10 decimal variables into descending order?

TheThingIs
The Singularity profile - One profile to rule them all and at HCS we bound them ;)

You see, TheThingIs, eventually you'll be allright.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Sorting numbers
« Reply #1 on: July 25, 2016, 07:52:39 PM »
That's a very general question, can you provide some context?

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2832
Re: Sorting numbers
« Reply #2 on: July 25, 2016, 08:36:33 PM »
Write a plugin ;)

TheThingIs

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 278
    • HCS Voicepacks
Re: Sorting numbers
« Reply #3 on: July 26, 2016, 01:45:08 AM »
lol a plugin, I knew that was going to be the answer but I was hoping it wouldn't be. I haven't programmed since 2001 but my profile is getting so complicated that it looks to be becoming necessary.

For context:
43.3 4.7 4.9 5.8 2.1 2.8 3.7 7.8 2.8 9.3

all individual variables which I need them to be in descending order. Basically in Elite, there is a modules panel which lists your modules in power usage order. I need to automate switching one of the modules off and one on that is off. The problem is that they are always in a different order depending on your ship loadout. I can get the list of modules and work out their power usage as a percentage of the power plant but I need to know where in that list is the module I want. Simples, yes? lol
The Singularity profile - One profile to rule them all and at HCS we bound them ;)

You see, TheThingIs, eventually you'll be allright.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Sorting numbers
« Reply #4 on: July 26, 2016, 04:05:08 AM »
I need to know where in that list is the module I want.

Code: [Select]
Set decimal [HighestValue] value to 0
Set decimal [Value1] value to 4,7
Set decimal [Value2] value to 43,3
Set decimal [Value3] value to 4,9
Begin Decimal Compare : [Value1] Is Greater Than [HighestValue]
    Set decimal [HighestValue] value to the value of [Value1]
    Set small int (condition) [HighestValuePosition] value to 1
End Condition
Begin Decimal Compare : [Value2] Is Greater Than [HighestValue]
    Set decimal [HighestValue] value to the value of [Value2]
    Set small int (condition) [HighestValuePosition] value to 2
End Condition
Begin Decimal Compare : [Value3] Is Greater Than [HighestValue]
    Set decimal [HighestValue] value to the value of [Value3]
    Set small int (condition) [HighestValuePosition] value to 3
End Condition
Write '[Purple] Highest value is {DEC:HighestValue} at Value{SMALL:HighestValuePosition}' to log
"Highest value is 43,30000 at Value2"


This is why context is important.

TheThingIs

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 278
    • HCS Voicepacks
Re: Sorting numbers
« Reply #5 on: July 26, 2016, 01:31:05 PM »
That could work, thanks Pfeil, you da man :)
The Singularity profile - One profile to rule them all and at HCS we bound them ;)

You see, TheThingIs, eventually you'll be allright.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Sorting numbers
« Reply #6 on: July 27, 2016, 12:47:40 AM »
That could work
For finding the highest value, yes, but I don't know why I was convinced that was all you needed.

What makes this entire thing a bit more verbose than it could be is the lack of variable variable names; I.E. you can't use "Set decimal [Value{SMALL:HighestValuePosition}] value to 0".

So, we have to work around that:

Code: [Select]
Set decimal [Value1] value to 4,7
Set decimal [Value2] value to 43,3
Set decimal [Value3] value to 4,9
-----------------------
Set small int (condition) [LoopCount] value to 1
Start Loop While : [LoopCount] Is Less Than Or Equals 3
    Set decimal [HighestValue] value to 0
    Begin Decimal Compare : [Value1] Is Greater Than [HighestValue]
        Set decimal [HighestValue] value to the value of [Value1]
        Set small int (condition) [HighestValuePosition] value to 1
    End Condition
    Begin Decimal Compare : [Value2] Is Greater Than [HighestValue]
        Set decimal [HighestValue] value to the value of [Value2]
        Set small int (condition) [HighestValuePosition] value to 2
    End Condition
    Begin Decimal Compare : [Value3] Is Greater Than [HighestValue]
        Set decimal [HighestValue] value to the value of [Value3]
        Set small int (condition) [HighestValuePosition] value to 3
    End Condition
    Begin Small Integer Compare : [HighestValuePosition] Equals 1
        Set decimal [Value1] value to -1
    Else If Small Integer Compare : [HighestValuePosition] Equals 2
        Set decimal [Value2] value to -1
    Else If Small Integer Compare : [HighestValuePosition] Equals 3
        Set decimal [Value3] value to -1
    End Condition
    Begin Small Integer Compare : [LoopCount] Equals 1
        Set small int (condition) [Panel1] value to the value of [HighestValuePosition]
    Else If Small Integer Compare : [LoopCount] Equals 2
        Set small int (condition) [Panel2] value to the value of [HighestValuePosition]
    Else If Small Integer Compare : [LoopCount] Equals 3
        Set small int (condition) [Panel3] value to the value of [HighestValuePosition]
    End Condition
    Set small int (condition) [LoopCount] value as incremented by 1
End Loop
Write '[Blue] Panel 1 at position {SMALL:Panel1}, Panel 2 at position {SMALL:Panel2}, Panel 3 at position {SMALL:Panel3}' to log

Now you have the position value of each panel, sorted highest to lowest(descending order).

Obviously you'll have to expand this to 10 panels, but you can see how it works.

Out of  convenience I set "LoopCount" to 1, so you don't have to worry about 0 indexing.

I'm setting to -1 as I'm assuming the lowest power usage would be 0. If it can be lower you'll need to change that accordingly.

Of course, the limitation of a sorting system is that values that are exactly the same cannot be differentiated, so hopefully that's not something that can occur.
« Last Edit: July 27, 2016, 12:51:03 AM by Pfeil »