locked
How to sort array? RRS feed

  • Question

  • User-1179262497 posted

    I have an array with these elements:

    [QP9982:2]

    [QP9982:3]

    [QP9983:2]

    [QP9983:4]

    [QP9984:1]

    [QP9985:2]

    What I want to produce is:

    [QP9982:2,3]

    [QP9983:2,4]

    [QP9984:1]

    [QP9985:2]

    Monday, August 8, 2011 1:29 AM

Answers

  • User-573138384 posted

    Tuned above code and this is the result...

                string result, key, val;
                List<string> input = new List<string> { "[QP9982:2]", "[QP9982:3]", "[QP9983:2]", "[QP9983:4]", "[QP9984:1]", "[QP9985:2]" };
                
                Dictionary<string, string> dict = new Dictionary<string, string>();
                for (int i = 0; i < input.Count; i++)
                {
                    result = input[i].ToString().Replace("[", string.Empty).Replace("]", string.Empty);
                    key = result.Split(':')[0];
                    val = result.Split(':')[1];
                    dict[key] = !dict.ContainsKey(key) ? val : dict[key] + "," + val;               
                }
                
                List<string> results = new List<string>();
                foreach (KeyValuePair<string, string> item in dict)
                {
                    results.Add("[" + item.Key + ":" + item.Value + "]");
                }
    
                results.Sort();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2011 2:39 AM

All replies

  • User-848116622 posted

    Loop this array and put it in hashtable with the following logic

     

    Hashtable ht = new Hashtable()

    for (Int32 i=0; i<arra.Length; i++)

    {

    String KeyVal = arra[i].ToString().Split(New Char()[':'])[0];

    String Val = arra[i].ToString().Split(New Char()[':'])[1];

    if (ht[KeyVal ]=Null)

    {

    ht[KeyVal]=Val

    }

    else

    {

    ht[KeyVal]=ht[KeyVal]+","+Val

    }

    }

    Please check the Syntax

    Monday, August 8, 2011 2:06 AM
  • User-573138384 posted

    Tuned above code and this is the result...

                string result, key, val;
                List<string> input = new List<string> { "[QP9982:2]", "[QP9982:3]", "[QP9983:2]", "[QP9983:4]", "[QP9984:1]", "[QP9985:2]" };
                
                Dictionary<string, string> dict = new Dictionary<string, string>();
                for (int i = 0; i < input.Count; i++)
                {
                    result = input[i].ToString().Replace("[", string.Empty).Replace("]", string.Empty);
                    key = result.Split(':')[0];
                    val = result.Split(':')[1];
                    dict[key] = !dict.ContainsKey(key) ? val : dict[key] + "," + val;               
                }
                
                List<string> results = new List<string>();
                foreach (KeyValuePair<string, string> item in dict)
                {
                    results.Add("[" + item.Key + ":" + item.Value + "]");
                }
    
                results.Sort();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2011 2:39 AM
  • User-1179262497 posted

    Thanks for that! it worked.

    Just last bit... is it possible to order each result item's value from smallest to big?

     

    Monday, August 8, 2011 4:12 AM
  • User-573138384 posted

    Yes.

    If you are using .net 3.0 or higher version then include this line...

    //replace
    results.Sort();
    
    //with
    results = results.OrderByDescending(r => r).ToList();

    Else it is 2.0 then, include this after results.Sort(); ...

                List<string> finalResult = new List<string>();
                for (int i = results.Count - 1; i >= 0; i--)
                {
                    finalResult.Add(results[i]);
                }

    Where as finalResult is your desired result...

    If it works close the thread.

    Monday, August 8, 2011 4:23 AM
  • User-1179262497 posted

    Hi fayaz_3e,

    What that's done was it ordered the elements by descending order.

    [QP9985:2]

    [QP9984:1]

    [QP9983:2,4]

    [QP9982:2,3]

    What I am trying to get is to order the elements' items by ascending order:

    [QP9982:2,3] //notice it;s ascending here for 2, 3...

    [QP9983:2,4] //ascending here for 2, 4...

    [QP9984:1]

    [QP9985:2]

    Monday, August 8, 2011 8:52 PM
  • User-573138384 posted

    Try this...

                string result, key, val;
                List<string> input = new List<string> { "[QP9982:2]", "[QP9982:3]", "[QP9983:2]", "[QP9983:4]", "[QP9984:1]", "[QP9985:2]" };
    
                Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
                for (int i = 0; i < input.Count; i++)
                {
                    result = input[i].ToString().Replace("[", string.Empty).Replace("]", string.Empty);
                    key = result.Split(':')[0];
                    val = result.Split(':')[1];
                    dict[key] = dict.ContainsKey(key) ? dict[key] : new List<string>();
                    dict[key].Add(val);
                }
                
                var items = (from item in dict.Values
                             from element in item
                             orderby element descending
                             select element).ToList();
    
                List<string> results = new List<string>();
                foreach (KeyValuePair<string, List<string>> item in dict)
                {
                    val = string.Join(",", item.Value.OrderByDescending(el => el).ToArray());
                    results.Add("[" + item.Key + ":" + val + "]");
                }
    
                results.Sort();

    If you want order by ascending change OrderByDescending in foreach loop to OrderBy.

    Tuesday, August 9, 2011 12:28 AM