1. Imagine the following code inside a plugin:


    Code:
    //references
    //various uses of namespacesusing DataHandling;namespace Oxide.Plugins
    {
        [Info("TestPlugin", "TheRotAG", "1.0.0")]
        public class TestPlugin : RustPlugin
        {
            //...some other vars
            private List<This> genericList;
            private string ThisData = "This_Data";
            private int getLastID { set; get; }        //...some other voids/classes/etc...
            this.genericList = Interface.GetMod().DataFileSystem.ReadObject<List<This>>(this.ThisData);       public void createThis(BasePlayer player, string name, string title)
           {     
                int sN = 1;
                foreach (This key in this.genericList)
                {
                    if (key.id.Equals(sN))
                    {
                        sN++;
                    }
                }
                getLastID = sN;
                This records = new This();
                records.generate(getLastID, player, name, title);
           }       //lots of other things that manages the Dafa File
         }
    }namespace DataHandling
    {
        public class This
        {
            public string name { set; get; }
            public string title { set; get; }
            public int id { set; get; }        public void generate(int id, BasePlayer someone, string title, string name)
            {
                this.id = id;
                this.someone = someone.userID.ToString();
                this.name = name;
                this.title = title;
            }
    }
    This code will generate a data that will look like this:

    [
    {
    "someone": "76561190000000000",
    "id": 1,
    "name": "foo" ,
    "title": "fox"
    },
    {
    "someone": "76561191111111111",
    "id": 3,
    "name": "bar" ,
    "title": "dog"
    },
    {
    "someone": "76561192222222222",
    "id": 2,
    "name": "lorem",
    "title": "ipsum"
    }
    ]


    What I need is sort each instance by its id.

    The reason is because each record is inserted following the id++ order, but when we delete one instance by its id (e.g. id 2) and create another, my code will compare all ids in the current order to elect the best id number to use. In the example data above, my code recognized that id 1 was already used, then checked if id 2 was free, since it wasn't there it created another instance with id 2 (the lorem ipsum one).

    Long history short, on my createThis class you will see that it checks all "dictionaries" (named instances by me before) until it finds a gap in the ids, so since the new instance with id 2 was created below the one with id 3 (remember that id 2 was deleted before, so that's why it was created below id 3 in the sequence), if I add another entry (instance) to the data file my logical comparison will fail by telling that id 3 is free and will create a second istance with id 3 as well.

    i.e. from public void createThis
    int sN = 1;
    foreach (This key in this.genericList)
    {
    if (key.id.Equals(sN))
    {
    sN++;
    }
    }
    it will compare id 3 with sN 2 and since they are different it will claim that id 3 its free to use...

    If sort isn't the best option here, I accept other id check suggestions :p
     
  2. What is the use case of the id on the class This (Which is terrible naming..), if the id is just so that it easier to store you should just use the users steam64Id.
     
  3. This its just and Example, I don't use it on my code...

    Each user can have various IDs in the data file, so each instance from the same users contains his SteamID and a different ID (the way of identification of each istance). The ID is a way to store the user's entered data to let other users reach it without knowing who entered. On my plugin those records NEEDS to be misterious to users other than the owner... those "other" users will catch all IDs in a list and will take for their selves the ID they wish to have, that way when they "take" that ID it is simply deleted from the data file, and how I don't want to store infinite IDs, I use the logical comparison to detect if a gap exists between ids, so a freed id can be used.
    [DOUBLEPOST=1424912064,1424886803][/DOUBLEPOST]Ok, since I couldn't find a way to Sort my data, seems like that I need to add another logical comparison, just to let the program confirm that the sN var isn't really equal any id on the data.
     
  4. Ok first of all I suggest you to a dictionary instead of list for better indexing, I recommend indexing the id as an int.
    So it would become: Dictionary<int,This> but inside This there would not be the id anymore.

    If you want to sort a list you can do that with this lambda expression:
    List.Sort((firstPair,nextPair) => { return ((int)firstPair.Value).CompareTo((int)(nextPair.Value)) ; } );

    To sort a Dictionary you can use the method OrderBy with the above expressions, just changing to what you need, or you can call .ToList() and then use the above to.

    Source: http://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value

    Hope it helps!