Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to use SortedList to store elements sorted in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedList<string, int> sorted_list = new SortedList<string, int>();

            sorted_list.Add("java", 1);
            sorted_list.Add("c++", 2);
            sorted_list.Add("python", 3);
            sorted_list.Add("c#", 4);
            sorted_list.Add("c", 5);

            foreach (KeyValuePair<string, int> kv in sorted_list)
                Console.WriteLine("{0} = {1}", kv.Key, kv.Value);
        }
    }
}


/*
run:

c = 5
c# = 4
c++ = 2
java = 1
python = 3

*/

 



answered Mar 6, 2017 by avibootz
0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedList<string, int> sorted_list = new SortedList<string, int>();

            sorted_list.Add("java", 1);
            sorted_list.Add("c++", 2);
            sorted_list.Add("python", 3);
            sorted_list.Add("c#", 4);
            sorted_list.Add("c", 5);
            sorted_list.Add("php", 6);

            bool b = sorted_list.ContainsKey("c++");
            Console.WriteLine("a. " + b);

            int i = sorted_list.IndexOfKey("c");
            Console.WriteLine("b. " + i);

            i = sorted_list.IndexOfValue(3);
            Console.WriteLine("c. " + i);

            Console.WriteLine("d. " + sorted_list.Count);

            Console.WriteLine("e. " + sorted_list["c#"]);

            if (sorted_list.TryGetValue("c", out i))
                Console.WriteLine("f. " + i);
        }
    }
}


/*
run:

a. True
b. 0
c. 5
d. 6
e. 4
f. 5

*/

 



answered Mar 6, 2017 by avibootz

Related questions

1 answer 119 views
119 views asked Dec 13, 2020 by avibootz
2 answers 227 views
1 answer 184 views
1 answer 165 views
1 answer 94 views
94 views asked Dec 13, 2020 by avibootz
1 answer 113 views
113 views asked Dec 13, 2020 by avibootz
...