5 Replies - 2747 Views - Last Post: 15 December 2011 - 09:28 AM Rate Topic: -----

#1 JohnYossarian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 11

Using LINQ and Random Numbers

Posted 14 December 2011 - 03:01 PM

I need to generate a 30 random number array and use linq to sort it . I have the following code so far, but it is not doing any of the sorting. Just shows the numbers. Any ideas?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sort_Arrays
{
    class Program
    {
        static void Main(string[] args)
        {
             Random rnd = new Random();
            var randomNumbers = Enumerable.Range(1, 30)
                                          .Select(x => new { val = x, order = rnd.Next() })
                                          .OrderBy(i => i.order)
                                          .Select(x => x.val)
                                          .ToArray();

            Display (values, "Random Numbers:");

            var sortAscending = 
                from value in randomNumbers
                orderby value 
                select value;

            Display(sortAscending,"Sorted(Ascending:");

            var sortDescending =
                from value in randomNumbers
                orderby value descending
                select value;

            Display(filtered, "Sorted(Descending:");

            

        }

        public static void Display( IEnumerable<int> results, string header)

    { 
      
        Console.Write("{0}", header);

        foreach (var element in results)
        Console.Write("{0}", element);

        Console.WriteLine();



        }
    }
}





Is This A Good Question/Topic? 0
  • +

Replies To: Using LINQ and Random Numbers

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: Using LINQ and Random Numbers

Posted 14 December 2011 - 03:13 PM

You don't seem to actually do a sort on your data.

http://msdn.microsof...array.sort.aspx
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4536
  • View blog
  • Posts: 7,915
  • Joined: 08-June 10

Re: Using LINQ and Random Numbers

Posted 15 December 2011 - 08:49 AM

I think it's because your code is a bit confused.

First, you're selecting the random number into "Order", and the index number into "Val." To me, that doesn't make any sense.

So, rewriting that part:

var numbers = Enumerable.Range(1, 30).Select(x => new {Value = rnd.Next(), Index = x}).ToArray();


Now we have an array of a generic type that contains an index number, and a random value.

Next, to sort ascending by the random number.

var asc = numbers.Select(x => x.Value).OrderBy(x => x);


Then, the same thing sorted by descending:

var desc = numbers.Select(x => x.Value).OrderByDescending(x => x);


Much simpler, no?

Btw, tlhIn`toq, you can use LINQ to sort (OrderBy and OrderByDescending, ThenBy and ThenByDescending), and it's somewhat simpler than Array.Sort, since you can define the comparison to sort by with a lambda, rather than an IComparer.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: Using LINQ and Random Numbers

Posted 15 December 2011 - 09:03 AM

I really am far behind the LINQ curve. So many cool software technologies, so little time.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4536
  • View blog
  • Posts: 7,915
  • Joined: 08-June 10

Re: Using LINQ and Random Numbers

Posted 15 December 2011 - 09:14 AM

LINQ is awesome. It's like...SQL for collections in memory, but better. Seriously, check it out. You have to learn to love lambdas, but once you understand them, you'll find more uses for them then you thought you could.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Using LINQ and Random Numbers

Posted 15 December 2011 - 09:28 AM

Curtis Rutland is talking about LINQ To Objects. Most of the "functionality" comes for the QueryProviders.

LINQ operates over anything that matches the correct pattern, its doesn't need to implement IEnumerable or IQueryable.

I think the best implementer of QueryProviders are from Bart De Smet


This post has been edited by AdamSpeight2008: 15 December 2011 - 09:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1