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

.Net C# Ascending sort of ArrayList with Hashtables

Hi,

I'm stuck with a problem...in .Net C# I want to sort an ArrayList containing Hashtables. The sort key must be the key of the Hashtable. How can I do this?

I've coded some example here...

Expand|Select|Wrap|Line Numbers
  1. ArrayList arr = new ArrayList();
  2. Hashtable row = new Hashtable();
  3. Hashtable row2 = new Hashtable();
  4. row.Add("name", "Naomi Watts");
  5. row.Add("age", "24");
  6. row2.Add("name", "Scarlett Johanson");
  7. row2.Add("age","16");
  8. arr.Add(row);
  9. arr.Add(row2);
  10.  
Now consider that I want to sort the ArrayList ascending on row["age"].
Sorting on IComparer works only when sorting on Objects, and I can't find a way for this situation.
Sep 27 '07 #1
11 4768
Shashi Sadasivan
1,435 Expert 1GB
You should be typecasting the object back to a hash table type

cheers.
PS: your structure dosent look so good!
Sep 27 '07 #2
How can I do that? Simply by "(Hashtable)..." or "as Hashtable"? Can you give an example please? And whats wrong with the structure? How would you do it in a situation where you have to save columns and rows?
Sep 27 '07 #3
Plater
7,872 Expert 4TB
IComparer interface has two objects come in, that you then type-cast to the object for sorting purposes.

As for your structure, there seems to be a lot of problems.
you are only using ONE hastable, never creating another one. So your arraylist is just going to have the same hastable in it everytime.

It rather seems that a DataTable would be more useful to you in this situation (define some colums, add values in a row) So instead of a new hashtable each time, you would just add another row to your DataTable.
Sep 27 '07 #4
Yes I saw it, thanks...It was only a sample code and in the actual source it's written right. I changed it now in the post. I could have use DataTables but me was said that this is a much faster solution :S

I will try to typecast and see if it works, thanks!
Sep 27 '07 #5
Small problem left :D don't know how to fill in the arr.Sort(...) and if it will work like this.

Thank you!

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace test
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             ArrayList arr = new ArrayList();
  13.             Hashtable row = new Hashtable();
  14.             Hashtable row2 = new Hashtable();
  15.             row.Add("name", "Naomi Watts");
  16.             row.Add("age", "24");
  17.             row2.Add("name", "Scarlett Johanson");
  18.             row2.Add("age", "16");
  19.             arr.Add(row);
  20.             arr.Add(row2);
  21.  
  22.             Array.Sort();   <----- How dow I fill this in?
  23.         }
  24.     }
  25.  
  26.     class sortAscending : IComparer
  27.     {
  28.        int IComparer.Compare(object a, object b)
  29.        {
  30.            //Typecast the object to Hashtables
  31.           Hashtable c1 = (Hashtable)a;
  32.           Hashtable c2 = (Hashtable)b;
  33.           if (Int32.Parse(c1["age"].ToString()) > Int32.Parse(c2["age"].ToString()))
  34.              return 1;
  35.          if (Int32.Parse(c1["age"].ToString()) < Int32.Parse(c2["age"].ToString()))
  36.              return -1;
  37.           else
  38.              return 0;
  39.        }
  40.  
  41.         public static IComparer sortAsc()
  42.         {
  43.             return (IComparer)new sortAscending();
  44.         }
  45.  
  46.     }
  47. }
  48.  
Sep 27 '07 #6
Plater
7,872 Expert 4TB
Try this:
Expand|Select|Wrap|Line Numbers
  1. Array.Sort(arr, new sortAscending());
  2.  
You don't need that static method in your sortAscending class.
Sep 27 '07 #7
Sorry, won't work...

1.The best overloaded method match for 'System.Array.Sort(System.Array, System.Array)' has some invalid arguments \Projects\test\Program.cs 22 13 test
2.Argument '1': cannot convert from 'System.Collections.ArrayList' to 'System.Array' \Projects\test\Program.cs 22 24 test
3.Argument '2': cannot convert from 'test.sortAscending' to 'System.Array' \Projects\test\Program.cs 22 29 test
Sep 27 '07 #8
Plater
7,872 Expert 4TB
Ooooo it's mad because of that, hehe oops

Expand|Select|Wrap|Line Numbers
  1. arr.Sort(new sortAscending());
  2.  
Sep 27 '07 #9
LOL! Plater it keeps complaining :(

1.The best overloaded method match for 'System.Array.Sort(System.Array)' has some invalid arguments \Projects\test\Program.cs 22 13 test
2.Argument '1': cannot convert from 'void' to 'System.Array' \Projects\test\Program.cs 22 24 test
Sep 27 '07 #10
Plater
7,872 Expert 4TB
You put in "arr" and not "Array" before the .Sort() right?
Sep 27 '07 #11
:$ sorry ... It now works perfectly! This surely made me learn a bit about IComparer. Thank you for your help!
Sep 27 '07 #12

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

Similar topics

0
by: renster | last post by:
I have been looking at implementing the following but with a difference. http://forums.devshed.com/t47653/s.html This example uses a link which works with some code shown on the page. I have...
5
by: Netmonster | last post by:
Hello, Does any one have an example of how to create an ArrayList of objects? i.e. ArrayList of ArrayLists or an ArrayList of Hashtables? Thanks in advance KC
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
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...
11
by: dwelden | last post by:
I have successfully used the sort lambda construct described in http://mail.python.org/pipermail/python-list/2006-April/377443.html. However, how do I take it one step further such that some values...
6
by: shana07 | last post by:
Phew, I have problem..How to sort number in my files..I have these in my input files...: I need to sort the line in array from 12, 64, 8, 128 etc. 3 12 4 64 7 8 10 128 ... I just wanna...
2
by: Bruce | last post by:
Hi I am having a problem understanding the exact advantages of using an ArrayList over a Hashtable in any situation. In most areas of an application I am working on, lookup needs to be fast. If I...
7
by: Kamran Shafi | last post by:
Hi, I am creating an arraylist (say masterArrayList) of hashtables, where each hashtable (say table) is of the format key=string, value = arraylist of strings (say existing_strings). In a...
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: 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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.