0

Ok, I'm looking to sort the List (UserList) by the property (WordList), although I need (WordList) to be sorted by a particular property first.

The whole Sort UserList method does not sort the UserList whether I try by (userName) or by the (WordList).(wordName) or (WordList).(amountOfGuesses)

public class User
{
    public string userName;
    public List<Word> wordList;

    public User() { }

    public User(string name, String word, int guesses)
    {
        this.userName = name;
        wordList = new List<Word>();
        Word theWord = new Word(word, guesses);
        this.wordList.Add(theWord);
    }
}

public List<User> SortUserList(String g) 
    {
        String option = g.ToLowerInvariant();
        switch(option){
            case "name":
                this.UserList.OrderBy(x=> x.userName);
                break;
            case "word":
                this.UserList.OrderBy(k => k.wordList.OrderBy(w => w.wordName));
                break;
            case "guess":
                this.UserList.OrderBy(m=> m.wordList.OrderBy(y => y.amountOfGuesses));
                break;
        } 
        return UserList;
    }

    public void Test()
    {
        User xor = new User("xor", "xadfs", 20);
        User bob = new User("bob", "char", 3);
        User james = new User("james", "adfsad", 200);
        UserList.Add(bob);
        UserList.Add(james);
        UserList.Add(xor);
    }
4
  • 1
    How do you expect to sort by a list? What would the wordList values be for your example? Could you paste the definition of User (its constructors and properties)? Commented Jun 21, 2014 at 8:54
  • @Douglas Pasted the Definition of User, and I want to sort UserList according to WordList already being sorted by a particular property Commented Jun 21, 2014 at 9:13
  • Will a user always only have just one word? If not, how would you sort when a user has several words? For example, if "xor" has "xadfs" and "aaa", whilst "bob" has just "char", how do you decide who comes first? Commented Jun 21, 2014 at 9:41
  • @Douglas Ooh I see the Issue here... So maybe I can approach it by first sorting the WordList by wordName in User, then get the First element of of WordList and sort the UserList that way. Commented Jun 21, 2014 at 9:59

2 Answers 2

2

If you want to sort the users by their first word (in chronological order), you can use Min instead of your inner OrderBy on the wordList sequence.

Another issue you have with your code is that you assume that the outer OrderBy will update your source UserList sequence. It doesn't; rather, it leaves the source unchanged, and returns a new query that enumerates the ordered sequence. You can populate this by calling ToList and returning the result.

case "name":
    return this.UserList.OrderBy(x => x.userName).ToList();
case "word":
    return this.UserList.OrderBy(k => k.wordList.Min(w => w.wordName)).ToList();
case "guess":
    return this.UserList.OrderBy(m => m.wordList.Min(y => y.amountOfGuesses)).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Douglas my problems have been solved. Appreciate your help
1

If you need to order by the "top" element of a list, you can use FirstOrDefault() to extract it:

UserList.OrderBy(k => k.wordList.OrderBy(w => w.wordName).FirstOrDefault());

I think this does what you want, I am not quite sure yet about what exactly you are asking though :-)

7 Comments

Yeah I'm going to need to take a similar approach as this, I have not tested this yet but something similar to this is what I'll have to do.
This will be inefficient. Never use a full sort when you only need the topmost element – it degrades your performance from O(n) to O(n log n).
@Douglas you are right, but don't make assumptions on efficiency or the implementation for operators. In most cases, it's likely irrelevant, anyways.
Thanks @Sklivvz your solutions have helped me solve my issues. I really appreciate the help !!
@Sklivvz: Yes, I did assume that the numbers would get large. If they don't, then you're right in saying that the performance difference will be likely irrelevant.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.