简体   繁体   中英

How to sort a List<DynamicTableEntity> a sealed class

I have a List of Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity that I need to sort. Although List provides me a Sort() method, DynamicTableEntity does not have a default comparator. As it is a sealed class, I can't seem to create my own.

I want to sort on DynamicTableEntity.RowKey.

What would be the most efficient way to sort this list? Should I roll my own sorting method or should I try to use some sort of Linq query, or is there something I'm missing?

You should be able to use the Sort override that has the Comparison<T> delegate.

Something like this:

entities.Sort((x,y) => /*Code to compare your two entities */);

Knowing that the delegate should return an int with the following rule:

  • -1 if the first element should be before second (x < y)
  • 0 if they are equal
  • 1 if the first element should be after the second (x > y)

The simple types already implement a CompareTo method that you're likely to be able to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM