Search Unity

Question Sort a list based on another List Id

Discussion in 'Scripting' started by Ekta-Mehta-D, Feb 15, 2021.

  1. Ekta-Mehta-D

    Ekta-Mehta-D

    Joined:
    Feb 25, 2013
    Posts:
    24
    Hello everyone..

    I have one Class having UserId.

    Code (CSharp):
    1.  [Serializable]
    2. public class Player
    3. {
    4.      public string flag;
    5.      public string userId;
    6.      public string userName;
    7.      public int coin;
    8. }
    I have another class with some different Property:

    Code (CSharp):
    1. public class Player_Game
    2. {
    3.      public string playerName;
    4.      public string userId;
    5.      public bool isUserPlayer;
    6. }
    I have collection of Player_Game and Player as well:

    Code (CSharp):
    1.  public List<Player_Game> players;
    2. public List<Player> players_JSON;
    I want to order players list based on userId of players_JSON List.

    I am not able to properly cast :

    Code (CSharp):
    1. players = players.OrderBy(x => players_JSON.FindIndex(x.userId)).ToList();
    i am getting error like x.userId can not convert from string to Player.
     
  2. Elango

    Elango

    Joined:
    Jan 27, 2016
    Posts:
    107
    Code (CSharp):
    1. players = players.OrderBy(x => players_JSON.FindIndex(y => y.userId == x.userId)).ToList();
     
  3. Ekta-Mehta-D

    Ekta-Mehta-D

    Joined:
    Feb 25, 2013
    Posts:
    24
    Thank you so much. Its working :)