Reputation: 1481
I am working in compact framework 3.5
myfile.cs
public class Cases : IEnumerable
{
private Hashtable cases = new Hashtable(); // hashtable initilized
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return cases.GetEnumerator();
}
public bool Add(string caseCode, string scanTime)
{
Reading reading = new Reading(caseCode, scanTime);
cases.Add(caseCode, reading);
//some other logic
}
}
I have initialized the hash table as cases.I am adding the scan barcode in cases variable.Currently it is not in any order.I need to order the scaned barcode code in ascending order.How to do this.
Update:
For example i am adding the following items as below,
Cases a = new Cases();
a.Add("11115", "2014-09-11T01:55:25.0000000-07:00");
a.Add("11111", "2014-09-11T01:55:40.0000000-07:00");
a.Add("11112", "2014-09-11T01:55:45.0000000-07:00");
a.Add("11110", "2014-09-11T01:55:56.0000000-07:00");
a.Add("11113", "2014-09-11T01:56:10.0000000-07:00");
a.Add("11111", "2014-09-11T01:56:17.0000000-07:00");
I want the data to be printed in same order as in list.Here the key what we are giving is time,here time is by default ascending order.But during printing this is printed like this,
[11110,StackOverflow.Reading]
[11111,StackOverflow.Reading]
[11111,StackOverflow.Reading]
[11112,StackOverflow.Reading]
[11113,StackOverflow.Reading]
[11115,StackOverflow.Reading]
I want this to be print like this,
[11115,StackOverflow.Reading]
[11111,StackOverflow.Reading]
[11112,StackOverflow.Reading]
[11110,StackOverflow.Reading]
[11113,StackOverflow.Reading]
[11111,StackOverflow.Reading]
Using Hash Table printing the above in random and not in any order.However SortedList is printing in ascending order.Can you tell me how to print the data in list in same order.
Upvotes: 2
Views: 5051
Reputation: 1180
Use Dictionary
.
The below program achieves your desired behavior - sorting by Reading.scanTime
.
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
Cases a = new Cases();
a.Add("keyA", "01:00");
a.Add("keyB", "11:30");
a.Add("keyC", "06:20");
foreach (KeyValuePair<string, Reading> x in a)
{
Console.WriteLine("{0}: {1}", x.Key, x.Value);
}
}
}
public class Cases : IEnumerable
{
private Dictionary<string, Reading> cases =
new Dictionary<string, Reading>();
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return cases.GetEnumerator();
}
public bool Add(string caseCode, string scanTime)
{
Reading reading = new Reading(caseCode, scanTime);
cases.Add(caseCode, reading);
cases = cases.Values.OrderBy(v => v).ToDictionary(r => r.caseCode);
return true;
}
}
public class Reading : IComparable
{
public string caseCode; // don't mind public fields and bad naming, it's just an example ;)
public string scanTime;
public Reading(string a, string b)
{
this.caseCode = a;
this.scanTime = b;
}
public int CompareTo(object obj)
{
Reading other = obj as Reading;
if (other == null)
{
return -1;
}
return this.scanTime.CompareTo(other.scanTime);
}
public override string ToString()
{
return caseCode + " scanned at " + scanTime;
}
}
}
Upvotes: 6
Reputation: 82
You can use SortedDictionary
. If I am not mistaken the default sorting is ASC. But you should consider performance (search time and memory use) aspects too. Have a look on DotNetPerls or MSDN articles.
Upvotes: 0
Reputation: 203842
The items in a HashTable
are inherently unordered. You cannot order them. You'll need to use an orderable collection if you want to have the items in a particular order. You can take the items from the collection order them, and put them somewhere else, but it won't affect the hash table itself.
Upvotes: 2