Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. KeyValuePair List problem?

KeyValuePair List problem?

Scheduled Pinned Locked Moved C#
helpalgorithmsdata-structuresquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Varun Sareen
    wrote on last edited by
    #1

    Dear Friends, I have a KeyValuePair List; which i am sorting according to the value added against the respective keys. Now after sorting the list acc. to values i need to fetch the similar key values into a string array (strL4[]). For this I have made a string type array and then i am iterating through each of the record of the KeyValuePair List and storing the key values in the string array. But during addition i need that the iteration must be performed once for all similar types of key values in List so that the string array contains only one distinct key value for multiple similar values.

    lstImport.Sort(delegate(KeyValuePairfirst,
    KeyValuePairsecond)
    {
    return first.Value.CompareTo(second.Value);
    });
    int i=lstImport.Count;
    string[] strL4= new string[i];
    foreach(KeyValuePair lstI in lstImport)
    {
    strL4[lstImport.Count - i] = lstI.Key;
    i--;
    }

    Can anyone help me out? Thanks Varun Sareen

    OriginalGriffO J 2 Replies Last reply
    0
    • V Varun Sareen

      Dear Friends, I have a KeyValuePair List; which i am sorting according to the value added against the respective keys. Now after sorting the list acc. to values i need to fetch the similar key values into a string array (strL4[]). For this I have made a string type array and then i am iterating through each of the record of the KeyValuePair List and storing the key values in the string array. But during addition i need that the iteration must be performed once for all similar types of key values in List so that the string array contains only one distinct key value for multiple similar values.

      lstImport.Sort(delegate(KeyValuePairfirst,
      KeyValuePairsecond)
      {
      return first.Value.CompareTo(second.Value);
      });
      int i=lstImport.Count;
      string[] strL4= new string[i];
      foreach(KeyValuePair lstI in lstImport)
      {
      strL4[lstImport.Count - i] = lstI.Key;
      i--;
      }

      Can anyone help me out? Thanks Varun Sareen

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      If what you are saying is that say your KVP list contains:

      K1, AAA
      K2, BBB
      K3, BBB
      K4, CCC

      Then your strings list should only contain 3 elements: "AAA", "BBB", and "CCC", then you need to: 1) Change your array of strings to a List<string> as you don't know how many elements it will contain - you can convert it to an array later if you need to with the ToArray method. 2) Set a string lastValue and set it to the first value, plus the string "Do not match" - this ensures that the first KVP value is transferred to the new list. 3) In your loop, compare the current KVP value to lastValue and if different, use the Add method to add it to your List of strings. 4) At the bottom of your loop, set lastValue to the KVP Value.

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • V Varun Sareen

        Dear Friends, I have a KeyValuePair List; which i am sorting according to the value added against the respective keys. Now after sorting the list acc. to values i need to fetch the similar key values into a string array (strL4[]). For this I have made a string type array and then i am iterating through each of the record of the KeyValuePair List and storing the key values in the string array. But during addition i need that the iteration must be performed once for all similar types of key values in List so that the string array contains only one distinct key value for multiple similar values.

        lstImport.Sort(delegate(KeyValuePairfirst,
        KeyValuePairsecond)
        {
        return first.Value.CompareTo(second.Value);
        });
        int i=lstImport.Count;
        string[] strL4= new string[i];
        foreach(KeyValuePair lstI in lstImport)
        {
        strL4[lstImport.Count - i] = lstI.Key;
        i--;
        }

        Can anyone help me out? Thanks Varun Sareen

        J Offline
        J Offline
        johannesnestler
        wrote on last edited by
        #3

        I think this pattern is what you are looking for:

        using System;
        using System.Collections.Generic;

        namespace KeyValuePair
        {
        class Program
        {
        static void Main(string[] args)
        {
        List<KeyValuePair<string, string>> listImport = new List<KeyValuePair<string, string>>();
        listImport.Add(new KeyValuePair<string, string>("K1", "CCC"));
        listImport.Add(new KeyValuePair<string, string>("K2", "BBB"));
        listImport.Add(new KeyValuePair<string, string>("K3", "AAA"));
        listImport.Add(new KeyValuePair<string, string>("K4", "BBB"));
        listImport.Sort(delegate(KeyValuePair<string, string> first, KeyValuePair<string, string> second)
        {
        return first.Value.CompareTo(second.Value);
        });

                // We only need to "remember" the last value because the KeyValuePair list is sorted.
                string strLast = String.Empty;
                List<string> listDistinct = new List<string>();
                foreach (KeyValuePair<string, string> kvp in listImport)
                {
                    if (strLast == kvp.Value)
                        continue;
        
                    listDistinct.Add(kvp.Value);
                    strLast = kvp.Value;
                }
        
                foreach (string str in listDistinct)
                    Console.WriteLine(str);
        
                Console.ReadKey();
            }
        }
        

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups