locked
ArrayList Sorting ??? RRS feed

  • Question

  • I have an arraylist with dups in it ex... ( 123, 321 ... could be in anyorder) i need to copy them (not remove..) to another array... whats the easiest way .. code sample please...

    Thursday, April 28, 2011 6:12 PM

Answers

  • Hi superlurker,

    After giving it some thought and walking away from the problem I had an Aha moment. The following code snippet will do what you need.

    Encoding enc = new UTF8Encoding(true, true);
    ArrayList arrayList = new ArrayList { 613, 123, 136, 243, 231, 312, 613, 123 };
    
    Int32[][] results = (from Int32 al in arrayList
               let value = enc.GetString( enc.GetBytes(al.ToString()).OrderBy (e => e ).ToArray())
               group al by value into numGroup
               where numGroup.Count() > 1
               select numGroup.ToArray()).ToArray();
           
    foreach(Int32[] dups in results)
    {
      Console.WriteLine("All numbers contain all the same digits : " + dups.Min ());
      foreach (Int32 element in dups)
      {
        Console.WriteLine("\t" + element.ToString());
      }
    }
    
     Done.

     Fernando

     

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by superlurker Tuesday, May 3, 2011 4:09 PM
    Tuesday, May 3, 2011 2:25 PM

All replies

  • two quick questions:

    1) Does it have to be ArrayList?  Why not List<>?

    2) your title says ArrayList Sorting but your question references copying.  Are you wanting sorting or not?


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    Thursday, April 28, 2011 6:29 PM
  • I try anything that works ....whats the best appoach?? it doesnt have to sort just copy dups...

     

    Thanks

    Thursday, April 28, 2011 6:47 PM
  • what version of .NET are you using?

    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    Thursday, April 28, 2011 6:53 PM
  • 3.5 .. Why??
    Thursday, April 28, 2011 6:59 PM
  • You can sort and copy a list like so:

     

        // Make a list of ints...

        List<int> original = new List<int> { 3, 5, 2, 132, 412, 5123, 31, 5 };

     

        List<int> sortedCopy = original.OrderBy(i => i).ToList();

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, April 28, 2011 7:16 PM
    Moderator
  • Hi superluker;

    Here is a Linq query which takes an ArrayList and finds dup values and puts them into an int array in sorted order. This solution needs at min VS 2008.

    // ArrayList with integers
    ArrayList al = new ArrayList { 613, 123, 243, 613, 123 };
    
    // Linq to Object query to find the dup integer values
    // and place them into arr int array in sorted order
    int[] arr = (from Int32 alist in al
           group alist by alist into alistGroup
           where alistGroup.Count() > 1
           orderby alistGroup.Key
           select alistGroup.Key ).ToArray<int>( );
    
    

    Fernando

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, April 28, 2011 7:24 PM
  • The best solution for sure is to use Generic list<T>, like James proposed. And there is plenty of methods available for it, to do sorting, ordering, selecting, .... with List<T>, especially when using Linq on it. If there is no any particular need to use ArrayList, I would strongly suggest you to swap it with the List<T>.


    Mitja
    Thursday, April 28, 2011 7:25 PM
  • was just wondering if the arraylist was a necessity due to a .NET version (1.1) to decide whether to give you a pre-LINQ solution or post-LINQ solution.

    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    Thursday, April 28, 2011 7:25 PM
  • Question what if the array looked like this ... ArrayList al = new ArrayList { 613, 123, 243, 316, 123 }; would it still get 613, 316 ??

    Thursday, April 28, 2011 7:35 PM
  • Not really not to me it just seams easier to use a sql or Linq query.... I could be wrong .... just looking at all options....
    Thursday, April 28, 2011 7:38 PM
  • No, in that case the only value placed into the int array would be 123 seeming it is the only value that is duplicated in the ArrayList.

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, April 28, 2011 7:49 PM
  • Thats what I need the most ... is there anyway to get it to search in anyorder dups???

    Thanks

     

     

    Thursday, April 28, 2011 7:55 PM
  • Are you saying that the integer values that are palindromes in your ArrayList are to be considered a duplicate value?
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, April 28, 2011 8:08 PM
  • Yes....
    Thursday, April 28, 2011 8:25 PM
  • If I understand, you need to get the duplicates of the numbers which are not exactlly equal to each other, but the number which include all the numbers too?

    Like in you example: 316, and 613? You consider these are equal, and must appear in the new list?


    Mitja
    Thursday, April 28, 2011 9:04 PM
  • Yep you got it now.... thats why i thought it was sql sort function or linq ??? (Still learning...LOL)

    Thursday, April 28, 2011 9:07 PM
  • You will need to use Permutation - to create for every number all possible combinations, to check every that combination with any other number in an array.

    A lot of more code. Will try to show an exmple.


    Mitja
    Thursday, April 28, 2011 10:05 PM
  • So you are NOT looking for palindromes as your answer to my last post. You are looking for all combinations of the digits. For example if the list was this:

    ArrayList al = new ArrayList { 613, 136, 316, 613, 123 };

    then the value 613 you would consider to have the values 613, 136, 316, 613 as all duplicates because they all have the same 3 digits 6, 1 and 3 in them.

    Is that what you are asking for?

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, April 28, 2011 10:24 PM
  • ArrayList al = new ArrayList { 613, 136, 316, 613, 123 };

    then the value 613 you would consider to have the values 613, 136, 316, 613 as all duplicates because ththeyave the digits 6, 1 and 3 in them.

    This is called Permutation!

    And I think he is looking for that.


    Mitja
    Thursday, April 28, 2011 10:27 PM
  • Yes... LOL
    Thursday, April 28, 2011 11:07 PM
  • Hi superlurker,

    >it just seams easier to use a sql or Linq query

    I don't think using Linq can more easier to achieve your goal. Your issue is an arithmetic issue. It will take community members much time to help you resolve this issue as community members are only volunteers in this forum. So it is better for you to write the arithmetic yourself and specific the problem when you meet during development.

    But based on your issue, I am not sure whether the bellow steps can help you.(If the original list A is <613, 136, 316, 613, 123 >)

    1. Sort the each number as <136,136,136,136,123> and store it in a new list named B.
    2. Then use the foreach ayntax to loop B and when it is the new number and get the index of the num in B, then according the index you can get the num in A

    Above is just my thought. You can refer to it.

    Thanks for understanding and supporting.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, May 3, 2011 7:04 AM
  • Hi superlurker,

    After giving it some thought and walking away from the problem I had an Aha moment. The following code snippet will do what you need.

    Encoding enc = new UTF8Encoding(true, true);
    ArrayList arrayList = new ArrayList { 613, 123, 136, 243, 231, 312, 613, 123 };
    
    Int32[][] results = (from Int32 al in arrayList
               let value = enc.GetString( enc.GetBytes(al.ToString()).OrderBy (e => e ).ToArray())
               group al by value into numGroup
               where numGroup.Count() > 1
               select numGroup.ToArray()).ToArray();
           
    foreach(Int32[] dups in results)
    {
      Console.WriteLine("All numbers contain all the same digits : " + dups.Min ());
      foreach (Int32 element in dups)
      {
        Console.WriteLine("\t" + element.ToString());
      }
    }
    
     Done.

     Fernando

     

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by superlurker Tuesday, May 3, 2011 4:09 PM
    Tuesday, May 3, 2011 2:25 PM
  • Thanks ....
    Tuesday, May 3, 2011 4:09 PM
  • Not a problem, glad I was able to help.

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, May 3, 2011 4:13 PM