d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Sort Method In Arraylist / C#
12Next
Add Reply New Topic New Poll
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
May 7 2013 09:42am
It's a kind of lottery ticket.

I can choose from 1 to 5 combinations.
Each combination are 6 unique random nu mber between 0 and 49 (included)


So, I have to sort from lowest to highter number in each line.

This is an example of what I got:

10-11-42-35-27-41-
27-9-40-36-38-4-
49-33-5-24-32-22-
45-9-11-21-38-6-
18-32-24-17-5-48-

And what I should get after Sort method :

10-11-27-35-41-42-
4-9-27-36-38-40-
5-22-24-32-33-49-
6-9-11-21-38-45-
5-17-18-24-32-48

Member
Posts: 927
Joined: Jul 26 2011
Gold: 0.00
May 7 2013 10:15am
Well, you could read each line, one at a time, and copy/move each value into a temporary arraylist. http://stackoverflow.com/questions/6655246/how-to-read-text-file-by-particular-line-separator-character

So we'd use "-" as the delimiter.

Then you'd have a temporary arraylist with :10, 11, 42, 35, 27, 41... which you could use any simple sorting method to sort (insertion, radix, bucket, etc.).

Overall, it'd look something like:

for each line:
1) copy line to temporary list X using "-" as a delimiter
2) sort that line
3) return/print the newly sorted arraylist
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
May 7 2013 11:09am
can you use lists? if so just make a list
Code
List<int> list = new List<int> {10,11,42,35,27,41 };


and call
Code
list.Sort()
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
May 7 2013 11:30am
Quote (OmegaJSP @ 7 May 2013 11:15)
Well, you could read each line, one at a time, and copy/move each value into a temporary arraylist. http://stackoverflow.com/questions/6655246/how-to-read-text-file-by-particular-line-separator-character

So we'd use "-" as the delimiter.

Then you'd have a temporary arraylist with :10, 11, 42, 35, 27, 41... which you could use any simple sorting method to sort (insertion, radix, bucket, etc.).

Overall, it'd look something like:

for each line:
1) copy line to temporary list X using "-" as a delimiter
2) sort that line
3) return/print the newly sorted arraylist


u mean another arraylist to temp each line then return value?

Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
May 7 2013 01:01pm
Quote (AbDuCt)
try this

Code
tickets = "10-11-42-35-27-41-".split(/-/)
tickets.sort_by { |ticket| ticket }.each { |ticket| puts ticket }

output:
Code
Process started >>>
10
11
27
35
41
42
<<< Process finished. (Exit code 0)
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
May 7 2013 02:24pm
thx guy found the solution here :

Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Lottery
{
   public partial class frmBillet : Form
   {
       int Nombre;
       bool Extra;
       ArrayList m_combination = new ArrayList();
       List<int> list = new List<int> {50, 50, 50, 50, 50 ,50};
 
       public frmTicket(int Nb)
       {
           InitializeComponent();
           Num = Nb;          
       }
 
       private void frmTicket_Load(object sender, EventArgs e)
       {            
           int i, j, Number;          
           Random R = new Random();
           bool unique = false;
           string Line = "";      

           for (i = 0; i < Num; i++)
           {
               for (j = 0; j < list.Count; j++)
                   list[j] = 50;
               Number = R.Next(0, 50);
               list[0] = Number;
               Line = Number + "-";
               for (j = 1; j < list.Count; j++)
               {
                   while (unique == false)
                   {
                       Number = R.Next(0, 50);
                       if (Number != list[0] && Number != list[1] && Number != list[2] && Number != list[3] && Number != list[4])                    
                           unique = true;                          
                     }                
                   unique = false;                    
                   list[j] = Number;                    
               }                
               list.Sort();
               for (j = 0; j < list.Count; j++)              
                   Line += list[j] + "-";                
               m_combination.Add(Line);          
           }            
           for (i = 0; i < m_combination.Count; i++)          
               lbNumero.Items.Add(m_combinaison[i]);        
 }
   }
}


This post was edited by eric838 on May 7 2013 02:25pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
May 7 2013 04:05pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
May 7 2013 04:34pm
Quote (AbDuCt)
this

Code
class Lottery
 def initialize( tickets )
   @tickets = tickets
   @ticket_array = Array.new(tickets) { Array.new(6) { 0 } }
 end

 def tickets_initialize
   @ticket_array.each do |tickets|
     tickets.each_index do |ticket|
       t = rand(49)
       redo if tickets.include? t
       tickets[ticket] = t
     end
   end
   @ticket_array.each { |ticket| ticket.sort_by! { |ticket| ticket } }
 end
 
 def to_a
   arr = []
   result = ""
   @ticket_array.each do |tickets|
     tickets.each { |ticket| result << "#{ticket.to_s}-" }
     arr.push result
     result = ""
   end
   arr
 end
end

test = Lottery.new 5
test.tickets_initialize
arr = test.to_a
puts arr[0]
puts "--------------------------------"
puts test.to_a
puts "--------------------------------"


output:
Code
Process started >>>
3-14-27-29-30-32-
--------------------------------
3-14-27-29-30-32-
4-7-18-19-27-36-
16-18-21-31-33-40-
12-15-19-28-30-37-
9-12-13-22-36-40-
--------------------------------
<<< Process finished. (Exit code 0)
================ READY ================


This post was edited by Azrad on May 7 2013 04:42pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 7 2013 04:48pm
lol did abduct get banned
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
May 7 2013 04:59pm
Quote (irimi @ May 7 2013 03:48pm)
lol did abduct get banned

naw he just copied some 0's and 1's onto his clipboard and his computer exploded
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll