Search Unity

Find index of lowest to highest numbers in a list

Discussion in 'Scripting' started by Griffo, Aug 17, 2017.

  1. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Hi, I have a List of 80 int the user can press any of the 80 buttons and each press ada 1 to that buttons index.

    What I'd like to do is find and save the index positions of the highest to lowest numbers in the List, is this possible, thanks.

    What I have is a Gin list, Country List, ABV List and a Price List.

    Code (CSharp):
    1. public List< string > gins = new List< string >();
    2. public List< string > country = new List< string >();
    3. public List< string > abv = new List< string >();
    4. public List< string > price = new List< string >();
    Then I populate them with a text file -

    5th Fire
    Spain
    42.00%
    £3.80

    Alkamist
    Spain
    40.00%
    £4.50

    Aviation
    USA
    40.00%
    £3.40

    Biercee
    Belgium
    44.00%
    £4.30

    Blackwood
    Scotland
    40.00%
    £3.60

    ect .. unto the 80 in the file

    then I have another List -

    Code (CSharp):
    1. public List< int > ginScore = new List< int >();
    That is next to the Gin column, so each time someone presses the button next to that Gin it gains 1 point in popularity, so I was looking for a way to save the index positions of the highest to lowest numbers in that List so I then could put them indexes of the gins List in order of popularity ..
     
    Last edited: Aug 17, 2017
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yes. But what happens if two buttons have the same number and they are both the lowest? or highest? or multiple buttons have the lowest or highest?
     
  3. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    @Brathnann Hi, thanks for the input.

    I'm not bothered about having multiples as I'm just going to then put another List in order of popularity depending on the button press count.

    Wouldn't the sorting just put, say two buttons are a value of 10, one after the other?

    like -

    index 0 = 3:
    index 4 = 6:
    index 12 = 10:
    index 21 = 10:
    index 34 = 27:
    index 56 = 29:
    index 70 = 54:
    index 79 = 63:

    then all the other index would come in as -

    index XX = 0:

    Thanks.

    EDIT - edited original question.
     
    Last edited: Aug 17, 2017
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Well, if you sort a list, the indexes change, so I don't think that is what you want. If you have a class that keeps track of your items and then has a field where you can apply the number of clicks, you could then sort it after. You could also make a dictionary so your key is the index and your clicks is the value.
     
  5. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    @Brathnann So if I go this way -

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class GinList : IComparable<GinList>
    6. {
    7.     public string _ginName;
    8.     public string _country;
    9.     public string _abv;
    10.     public string _price;
    11.     public int _pop;
    12.  
    13.     public GinList(string ginName, string country, string abv, string price, int pop)
    14.     {
    15.         _ginName = ginName;
    16.         _country = country;
    17.         _abv = abv;
    18.         _price = price;
    19.         _pop = pop;
    20.     }
    21.     public int CompareTo(GinList other)
    22.     {
    23.         if(other == null)
    24.         {
    25.             return 1;
    26.         }
    27.  
    28.         // return the difference in popularity.
    29.         return _pop - other._pop;
    30.     }
    31. }
    32.  
    And use this to input the data -

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class GinListClass : MonoBehaviour
    6. {
    7.     void Start ()
    8.     {
    9.         List<GinList> gins = new List<GinList>();
    10.  
    11.         gins.Add( new GinList("Aviation", "USA", "40.00%", "£3.40", 2));
    12.         gins.Add( new GinList("5th Fire", "Spain", "42.00%", "£3.80", 1));
    13.         gins.Add( new GinList("Alkamist", "Spain", "40.00%", "£4.50", 0));
    14.  
    15.         gins.Sort();
    16.  
    17.         Debug.Log("   Gin              Country              ABV              Price              Popularity");
    18.  
    19.         foreach(GinList gin in gins)
    20.         {
    21.             Debug.Log(gin._ginName + "            " + gin._country + "                " + gin._abv +
    22.                       "           " + gin._price + "          " + gin._pop);
    23.         }
    24.     }
    25. }
    The output shows it's working, right?

    Screen Shot 2017-08-17 at 20.16.42.png

    Just need to use -

    Code (CSharp):
    1. gins.Reverse();
    I'll remove -

    Code (CSharp):
    1. gins.Add( new GinList("Aviation", "USA", "40.00%", "£3.40", 2));
    2. gins.Add( new GinList("5th Fire", "Spain", "42.00%", "£3.80", 1));
    3. gins.Add( new GinList("Alkamist", "Spain", "40.00%", "£4.50", 0));
    And read the text file in here ..

    Thanks for pointing me in the right direction, much appreciated ..