473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to Sort an Array of Objects?

Hi everybody,
i'm new to this forum and also to C#.
I hope someone with more experience can help me with my problem.
Given is the following Class
Expand|Select|Wrap|Line Numbers
  1.         public class movie
  2.         {
  3.             public long ID { get; set; }
  4.             public string NAME { get; set; } // 0
  5.             public string SIZE { get; set; } // 1
  6.             public string TYPE { get; set; } // 2
  7.             public DateTime CHANGEDATE { get; set; } // 3
  8.             public DateTime CREATIONDATE { get; set; } // 4
  9.             public DateTime LASTUSED { get; set; } // 5
  10.             public string ARCHIVE { get; set; }
  11.             public string PATH { get; set; }
  12.  
  13.             public movie() { }
  14.         }
I have an Array of movie Objects, now i wanted to sort the Array by "ARCHIVE" which contains a string like "HDD 1" or "DVD - 145".

The sorting is quite good with this line of code:
Expand|Select|Wrap|Line Numbers
  1. Array.Sort(MovieArray, (x, y) => string.Compare(x.ARCHIVE, y.ARCHIVE, true));
But the Problem is that when all Movies have the same ARCHIVE-String (because they are all on the same Medium) the Names of the Movies will be totaly unsorted after sorting by ARCHIVE.
It looks like this:

Expand|Select|Wrap|Line Numbers
  1. Before sorting:
  2. ["Movie A", "HDD1"]
  3. ["Movie B", "HDD1"]
  4. ["Movie C", "HDD1"]
  5. ["Movie D", "HDD1"]
  6. ["Movie E", "HDD1"]
  7.  
  8. After Sorting:
  9. ["Movie D", "HDD1"]
  10. ["Movie E", "HDD1"]
  11. ["Movie A", "HDD1"]
  12. ["Movie B", "HDD1"]
  13. ["Movie C", "HDD1"]
  14.  
Does anyone have a hint how to sort first by ARCHIVE and after that by NAME?
What i generally want is:
Expand|Select|Wrap|Line Numbers
  1. ["Movie A", "HDD1"]
  2. ["Movie B", "HDD1"]
  3. ["Movie C", "HDD1"]
  4. ["Movie D", "HDD1"]
  5. ["Movie E", "HDD1"]
  6. ["Movie A", "HDD2"]
  7. ["Movie B", "HDD2"]
  8. ["Movie C", "HDD2"]
  9. ["Movie D", "HDD2"]
  10. ["Movie E", "HDD2"]
  11. ["Movie A", "HDD4"]
  12. ["Movie B", "HDD4"]
  13. ["Movie C", "HDD4"]
  14. ["Movie D", "HDD4"]
  15. ["Movie E", "HDD4"]
  16.  
Thank you for reading my Post and hopefully you can help me with this.

greetings regalis
Mar 18 '10 #1

✓ answered by Christian Binder

Hy,

if you're able to use LINQ, you can do it that way:

Expand|Select|Wrap|Line Numbers
  1. //MovieArray has type movie[]
  2. movie[] ordered1 = MovieArray.OrderBy(mov => mov.ARCHIVE).ThenBy(mov => mov.NAME).ToArray();
just the same, only in other notation:

Expand|Select|Wrap|Line Numbers
  1. movie[] ordered2 = (from mov in MovieArray
  2.                     orderby mov.ARCHIVE, mov.NAME
  3.                     select mov).ToArray();

2 2103
Christian Binder
218 Expert 100+
Hy,

if you're able to use LINQ, you can do it that way:

Expand|Select|Wrap|Line Numbers
  1. //MovieArray has type movie[]
  2. movie[] ordered1 = MovieArray.OrderBy(mov => mov.ARCHIVE).ThenBy(mov => mov.NAME).ToArray();
just the same, only in other notation:

Expand|Select|Wrap|Line Numbers
  1. movie[] ordered2 = (from mov in MovieArray
  2.                     orderby mov.ARCHIVE, mov.NAME
  3.                     select mov).ToArray();
Mar 18 '10 #2
Hi ChBinder,
thank you very much for your help.
I like the way you have done the sorting and it works perfect!
100000 hugs to you :)

greetz regalis
Mar 18 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
7
by: Daniel | last post by:
does C# have any collection objects that support sort functionality so that I dont have to write my own sorting algorithm?
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: fdu.xiaojf | last post by:
Hi, It seems that an array acts like an list very much, except it doesn't have a method sort. Regards,
2
by: John Devlon | last post by:
Hi, I've created my own class containing a few properties like name, zip-code and savings. At some point i'm storing several objects in an array. Does anyone know how to sort the array using...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.