This site requires JavaScript, please enable it in your browser!
Greenfoot back
erchainis
erchainis wrote ...

2018/4/24

Sorting arrays

erchainis erchainis

2018/4/24

#
 private String strFile;
    private int xPos;
    private int yPos;
    private int counter;
    private RideName myList[];
    private String myListName[];
    private FileDialog fd;
    
    /**
     * Act - do whatever the Load wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     public void performAction()
     {
        openFile();
      
     }
     
    public Load()
    {
        super("Load");
        
        myList = new RideName[14];
    }
    
   
    
    public void openFile()
    {
        World MyWorld = getWorld();
        fd = null;
        fd = new FileDialog(fd, "Pick a file with data", FileDialog.LOAD);
        fd.setVisible(true);
        String fullName = fd.getDirectory() + fd.getFile();
        File dataFile = new File(fullName);
        
        Scanner Reader = null;
        try
            {
            
                Reader = new Scanner(dataFile);
                
                World w = getWorld();
                
                MyWorld wor = (MyWorld) getWorld();
                
                wor.removeAllButtons();
                
                xPos = MyWorld.getHeight()-75;
                
                yPos = 55;
                
                counter = 0;
                
                strFile = null;
                
                    while(Reader.hasNext())
                    {
                          strFile = Reader.nextLine();
                          
                          if(counter < myList.length)
                          {
                              w.addObject(myList[counter] = new RideName(strFile), xPos+120, yPos);
                          }
                          
                          yPos+=40;
                          counter++;
                    }
            }
        catch(FileNotFoundException fnfe)
             {
                 
             }
    }
    
    public void mergeFile()
    {
        World MyWorld = getWorld();
        fd = null;
        fd = new FileDialog(fd, "Pick a file with data", FileDialog.LOAD);
        fd.setVisible(true);
        String fullName = fd.getDirectory() + fd.getFile();
        File dataFile = new File(fullName);
        
        Scanner Reader = null;
        try
            {
            
                Reader = new Scanner(dataFile);
                
                World w = getWorld();
                
                MyWorld wor = (MyWorld) getWorld();
             
                
                    while(Reader.hasNext())
                    {
                          strFile = Reader.nextLine();
                          
                          if(counter < myList.length)
                          {
                              w.addObject(myList[counter] = new RideName(strFile), xPos+120, yPos);
                          }
                          
                          yPos+=40;
                          counter++;
                    }
            }
        catch(FileNotFoundException fnfe)
             {
                 
             }
    }
I have an array of RideName class that is this one:
public RideName(String txt) 
    {
        
        rideName = txt;
        setImage(new GreenfootImage(rideName, 13, Color.BLUE, Color.WHITE)); 
    }    
    
    protected void addedToWorld(World w)
    {
        
        a = new Add(this);
        
        getWorld().addObject(a, this.getX()-60, this.getY());
        
            while(this.intersects(a))
               {
                 
                 setLocation(getX()+4, getY());
                 
               }
    }
So far my code works as I want it. However, I would like now to know how to sort the array of RideName by the string of each one., but I do not know how. I tried to used the method toString(), but it was not helpful at all. Althoug I also have an arrays of String that it also contain the data. But I do not know what to do now.
danpost danpost

2018/4/24

#
erchainis wrote...
I also have an arrays of String that it also contain the data. But I do not know what to do now.
You could simply use:
java.util.Collections.sort(java.util.Arrays.asList(myListName));
You need to login to post a reply.