1

I have 2 generic lists.

  Dim Item As New List(Of String)
  Dim Item2 As New List(Of Array)

Item conatains 4 elements.

They are 1. Item code 2. Description 3. BIN 4. Price

But they are just entered sequentially For example

Item={5858545, HairDryer, A45, 50}

There are many Item and Item 2 contains The list of Items. For example Item2= {{5858545, HairDryer, A45, 50},....}

Now i want to sort the Item2 according to according to the BIN and then By Item code

So now my list Item2 will contains elements of Item sorted according to BIN and then By Item code.

How to do this with LINQ or any other way?

Thanks

2
  • 1
    I would strongly recommend creating a class for Item1 instead of an array of strings. It will make your code much cleaner. Commented Feb 12, 2013 at 14:56
  • You use an object-oriented language, you should make a better use of objects in your code. For example, you can create an Item class with properties ItemCode, Description, Bin, Price... This will greatly simplify your code. Commented Feb 12, 2013 at 14:58

1 Answer 1

4

You shouldn't be storing objects as an array or list of values. Create a new class to represent your item. Give it four properties for BIN, name, etc., and then create a list of that custom type.

To order the list you can use the OrderBy method, followed by the ThenBy method. It will read much better if you have a custom object, as it will look like:

list.OrderBy(item => item.BIN).ThenBy(item => item.Code);

instead of the much uglier option you need to do if you have arrays/lists:

list.OrderBy(item => item[0]).ThenBy(item => item[1]);

Note how the first example reads exactly like your requirements. "Order the list by BIN and then by Code." It's perfectly clear to someone looking at the code for the first time exactly what it's doing.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes i know this is bad to use list but the main problem is if i ahve to change the list to class then i have to change in many parts of the code
@user2008654 That's why you should make an effort to add designs like this at the start. Even so though, if this is used in a lot of places it's all the more important that a class be used instead of an array. Currently the code is most likely a mess, very hard to comprehend and reason able, much harder to maintain and expand on, and lacking a lot of compile time checking that it ought to have.
Thanks very much it worked i will try to implement it as class

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.