473,386 Members | 1,817 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,386 software developers and data experts.

Sorting List<int> low to high, then sorting other lists based on that

HaLo2FrEeEk
404 256MB
I cannot seem to even think of a way I might do this. Basically I have 6 lists with various pieces of information about the data I'm processing. I need to sort one of the lists (the one containing the date/time information) and have the rest of the lists sort the same exact way.

So let's say I have these 2 lists:

List<int> dates = new List<int>() { 56789, 01234 };
List<string> names = new List<string>() { name1, name2 };

I sort the "dates" list so that it's ordered from lowest to highest:

01234
56789

Then the names list needs to adjust accordingly, meaning it would sort to:

name2
name1

How would I go about doing something like this?

In PHP there are associative arrays that made this simple, but with this many lists it'd still be a huge pain.

Any help will be most appreciated, I can't really continue with my program until I sort this out.
May 16 '10 #1
6 3285
try dictionary/hashes with "list" as dictionary.items, in other words nest your lists into a dictonary/hash object. then sorting all lists become very easy. or nest dictionary inside dictionary. sorry, i don't know c# but I would nest dictionaries in python3.
May 16 '10 #2
HaLo2FrEeEk
404 256MB
I ended up creating a new class to hold all my information:

Expand|Select|Wrap|Line Numbers
  1. public class screenshotItem
  2. {
  3.     public string fileName { get; set; }
  4.     public string shotTitle { get; set; }
  5.     public int gameID { get; set; }
  6.     public int jpegOffset { get; set; }
  7.     public int jpegLength { get; set; }
  8.     public int fileTime { get; set; }
  9. }
Then I made 1 list:

List<screenshotItem> screenshots = new List<screenshotItem>();

And to add to that list:

Expand|Select|Wrap|Line Numbers
  1. screenshotItem screenshot = new screenshotItem() {
  2.     fileName = finfo.FullName,
  3.     shotTitle = shotTitle,
  4.     gameID = gameID,
  5.     jpegOffset = jpegOffset,
  6.     jpegLength = jpegLength,
  7.     fileTime = fileTime};
  8. screenshots.Add(screenshot);
Now to sort:

Expand|Select|Wrap|Line Numbers
  1. IEnumerable<screenshotItem> sortedScreenshots =
  2.     from screenshot in screenshots
  3.     orderby screenshot.fileTime
  4.     select screenshot;
This way beats having 6 individual Lists, then the hacked together code I was using to be able to display all the items, etc. It's much cleaner and I can sort it perfectly.
May 17 '10 #3
Curtis Rutland
3,256 Expert 2GB
@HaLo2FrEeEk
A quicker version of this (with a more usable result) is this:
Expand|Select|Wrap|Line Numbers
  1. List<screenshotItem> sorted = screenshots.OrderBy(x => x.fileTime).ToList();
You do it all on one line, skip the select, and end up with a list instead of an IEnumerable (lists are easier to work with, all though all IEnumerables support the ToList()).
May 17 '10 #4
HaLo2FrEeEk
404 256MB
Thanks, I'll look into that, but I'd still display the items the same way:

Expand|Select|Wrap|Line Numbers
  1. foreach (screenshotItem shot in sortedScreenshots)
  2. {
  3.     string fileName = shot.fileName.Remove(0, shot.fileName.LastIndexOf(@"\") + 1);
  4.     ListViewItem lvi = new ListViewItem(fileName);
  5.     lvi.SubItems.Add(shot.shotTitle);
  6.     lvi.SubItems.Add(gamesTitles[shot.gameID]);
  7.     lvi.SubItems.Add("0x" + shot.jpegOffset.ToString("X"));
  8.     lvi.SubItems.Add(shot.jpegLength.ToString());
  9.     lvi.SubItems.Add(fatXDate(shot.fileTime).ToString());
  10.     fileList.Items.Add(lvi);
  11. }
It would be nice to have it on 1 line though.
May 17 '10 #5
jkmyoung
2,057 Expert 2GB
Another way I've seen to do this is to have inherit the class from IComparable, eg:
Expand|Select|Wrap|Line Numbers
  1. public class screenshotItem : IComparable
  2. {
  3.  ... 
  4.          public int CompareTo(Object o)
  5.         {
  6.             if (o.GetType() != typeof(screenshotItem))
  7.               return 0;
  8.             return fileTime - ((screenshotItem)o).fileTime;
  9.         }
  10.  
Then the List.Sort() methods can still be used as before.
May 17 '10 #6
Curtis Rutland
3,256 Expert 2GB
@jkmyoung
Right. My version is basically taking one property of the object to sort by and using the built in comparison. If you wanted to use my way to sort the list based on an custom object property, they would also have to implement IComparable.
May 17 '10 #7

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

Similar topics

2
by: John Dalberg | last post by:
I am adding integer values from a datarow collection to a List<int> using the Add method. The values are read in the correct order from the collection but once the List is populated, the values in...
2
by: deathtospam | last post by:
Is it possible to Databind to a strongly-typed list of integers (List<int>) ? I want to use <%# Databinder.Eval(Container.DataItem, "XXX") %inside a Repeater control in my ASPX page, but I don't...
2
by: per9000 | last post by:
Hi, *background* I want a class containing an int (a list of sets of integer). This should be hidden for the user and he/she should be able to insert his/her favourite data structure so to be a...
8
by: per9000 | last post by:
Hi, I wanted to test to compile an application I build for .NET 2.0 in with the 1.1 C# compiler. I encountered difficulties since I had a List<myClass>. I found a list of what is new in .NET 2.0...
1
by: Monty | last post by:
I have a Usercontrol with a public property List<intLinks List <intLinks= new List<int>(); public List<intLinkLabels { get { return Links; } set { Links = value; } }
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
2
by: =?Utf-8?B?QW50?= | last post by:
Hi, I found in MSDN some sample code: List<intnumQry = ...etc I couldn't find any info in MSDN on List or <intthough. Does anybody know what this is & in which namespace it belongs in? ...
0
by: DR | last post by:
System.Collections.Generic.List<intmyList = new System.Collections.Generic.List<int>(100); Does this preallocate 100 integers? Also, is there any way to preallocate an array of objects all at...
6
by: chandramohanp | last post by:
Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member. Following is the code. class...
15
by: hsachdevah | last post by:
Hello, I am trying to create a dictionary item with its key as list type and value as custom object type. It gives me a error "An item with the same key has already been added." Here is my code:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.