0

I have a class that contains a lot of fields. I want to order the list of items of my class by one property, then by the other and so on... I need to do that using SortExpression and SortDirection.

How can I do this in vb?

Dim LsDocuments As List (Of clsDoc) = GetDocuments() 

clsDoc is a class with properties:

Date
Hour
Key
Office

I need something like: LsDocuments.orderby("Date","Asc"), and not like LsDocuments.orderby(Functuion(x) x.Date) because it's not general- every time I need to sort by other property (and doing Select case is very not classic).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
user2162278
  • 67
  • 1
  • 2
  • 10
  • 2
    The terms sortexpression and sortdirection are related to a `GridView`, is your question related to an ASP.NET `GridView` only? You should show us some code anyway. – Tim Schmelter Mar 04 '14 at 08:15
  • yes, right now I really am using a dridview. But my question is general -also for times not related to gridview. I just need a way to do a list orderby not by a specific propert- but by expression. – user2162278 Mar 04 '14 at 08:21
  • Your question is _too_ general, you should show us what you're trying and where you've stuck. Otherwise it is in danger of being closed as _too broad_. – Tim Schmelter Mar 04 '14 at 08:28
  • Dim LsDocuments As List (Of clsDoc) = GetDocuments() clsDoc is a class with properties: Date,Hour,Key,Office... I need something like: LsDocuments.orderby("Date","Asc"), and not like LsDocuments.orderby(Functuion(x) x.Date) because it's not general- evry time I need to sort by other property (and doing Select case is very not classic). Sorry I can't post more code- the connection to the internet from where I work doesn't let me doing copy/paste... – user2162278 Mar 04 '14 at 08:41
  • Don't post code in comments but edit your question with the relevant informations. I've edited your question but you should include more (f.e. types). Apart from that, does `GetDocuments` return the objects from database? If so, why don't you order them in the first place? – Tim Schmelter Mar 04 '14 at 08:43
  • the information comes from database- from procedures in natural language (not sql) I'm sure it's easier to sort in vb then in natural. And yet- after It comes from database, the user should be able to sort- can't do that only in first place... – user2162278 Mar 04 '14 at 08:52

1 Answers1

0

What I often do is create a property that contains all the values as a string and then sort by that property. Just make sure your string is sortable by making it fixed width. For example:

Public Readonly Property Sort1 as string
 Get
  Return Date.Tostring("s") & Hour.Tostring.PadLeft(2,"0") & Key.PadRight(50," ") & Office.PadRight(50," ")
 End Get
End Property
Steve
  • 5,585
  • 2
  • 18
  • 32
  • nice solution. still restricted- the order of properties to sort by is fixed- if you want to change the sort expression, you must create new property. But still- creative solution... – user2162278 Mar 10 '14 at 10:54
  • You can also create your own sort methods that would allow you to pass multiple properties at runtime by implementing the IComparable interface. If you use Generics, you could use Linq like this example: http://stackoverflow.com/questions/869438/sort-generic-list-on-two-or-more-values – Steve Mar 10 '14 at 14:12