A List<KeyValuePair<int,float>> , and a custom sorter will also work. the key for each pair contains the source index.
private void Form1_Load(object sender, EventArgs e) { List<KeyValuePair<int,float>> data = new List<KeyValuePair<int,float>> { new KeyValuePair<int,float>(0,1.5f), new KeyValuePair<int,float>(1,2), new KeyValuePair<int,float>(2,0), new KeyValuePair<int,float>(3,0.4f), new KeyValuePair<int,float>(4,-1), new KeyValuePair<int,float>(5,96), new KeyValuePair<int,float>(6,-56), new KeyValuePair<int,float>(7,8), new KeyValuePair<int,float>(8,-45) }; data.Sort(SortByValue); foreach (KeyValuePair<int, float> kv in data) { listBox1.Items.Add(kv.Key.ToString() + " - " + kv.Value.ToString()); } } private int SortByValue(KeyValuePair<int, float> a, KeyValuePair<int, float> b) { return a.Value.CompareTo(b.Value); }
source share