How to print sentences from the smallest to the largest?

2

I need to make a class that takes 3 user phrases and prints in order from the shortest sentence to the longest:

  

Make a program to read three user phrases and display on the screen the   three sentences in order of sentence size. For example: Suppose the   user provided the following phrases:

     
  • Today I was happy
  •   
  • Today is Friday
  •   
  • Yesterday I was very sad
  •   

    The result of the program would be:

         
    Hoje é sexta
    Hoje eu fiquei alegre
    Ontem eu estava bem triste
    

    I've tried 2 ways:

    1: Using array and Collections.sort that I searched the net and could not even understand

    package exercicio1;
    
    import java.util.ArrayList;
    import java.util.Collections;
    
    import javax.swing.JOptionPane;
    
    public class App {
    
        public static void main(String[] args) {
    
    
            ArrayList<String> lista = new ArrayList<String>();
            lista.add(JOptionPane.showInputDialog("Frase 1:"));
            lista.add(JOptionPane.showInputDialog("Frase 2:"));
            lista.add(JOptionPane.showInputDialog("Frase 3:"));
    
            Collections.sort(lista);
    
            for(int i = 0; i < lista.size(); i++){
                System.out.println(lista.get(i));
            }
    
        }
    }
    

    2: Using a vector of strings and ifs. I was able to print the smallest of all but I have no idea how to proceed, I could facilitate this process using Math.min (phrase1.length (), Math.min (phrase2.length (), frase3.length ()) ;

    package exercicio1;
    
    import javax.swing.JOptionPane;
    
    public class App {
    
        public static void main(String[] args) {
    
    
            String[] frases = new String[3];
    
            String frase1 =  JOptionPane.showInputDialog("Frase 1:");
            String frase2 =  JOptionPane.showInputDialog("Frase 2:");
            String frase3 =  JOptionPane.showInputDialog("Frase 3:");
    
            if(frase1.length() < frase2.length() && frase1.length() < frase3.length()){
                frases[1] = frase1;
            }else{
                if(frase2.length() < frase1.length() && frase2.length() < frase3.length()){
                    frases[1] = frase2;
                }else{
                    if(frase3.length() < frase1.length() && frase3.length() < frase2.length()){
                        frases[1] = frase3;
                    }
                }
            }
            System.out.println(frases[1]);
    
        }
    }
    

    I've been trying to do all day, I do not know how a seemingly so easy exercise is taking me so long.

        
    asked by anonymous 06.10.2018 / 23:39

    1 answer

    5

    The first form you used is the simplest one, however, Collections.Sort() used with   Comparator String default will only sort alphabetically.

    To organize by string size, you need to create a Comparator of itself so that the sort() method knows how to organize the list.

    The form below, removed from this answer in SOEn , creates% with% that returns the subtraction of the two strings at once which are being compared:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    
    import javax.swing.JOptionPane;
    
    public class App {
    
        public static void main(String[] args) {
    
    
            ArrayList<String> lista = new ArrayList<String>();
            lista.add(JOptionPane.showInputDialog("Frase 1:"));
            lista.add(JOptionPane.showInputDialog("Frase 2:"));
            lista.add(JOptionPane.showInputDialog("Frase 3:"));
    
            Collections.sort(lista, new Comparator<String>() {
    
                @Override
                public int compare(String o1, String o2) {
                    // TODO Auto-generated method stub
                    return o1.length() - o2.length();
                }
            });            
    
            for(int i = 0; i < lista.size(); i++){
                System.out.print(lista.get(i));
                System.out.println(" - tamanho: " + lista.get(i).length());
    
            }
    
        }
    }
    

    The operation is simple: if the result of the subtraction is 0, it means that both strings are the same size, in which case Comparator will not do anything. If the result is greater than 0, it means that the first string is greater than the second string, so the sort() will change them. If the result is less than zero, it means that the first string is less than the second, in which case the sort() probably will not do anything either.

    To illustrate how the code works, see the test below. I changed the dialog boxes for only text-mode entries and I recommend that you do not mix graphical interface with text:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
    
    public class App {
    
        public static void main(String[] args) {
    
            Scanner scan = new Scanner(System.in);
    
            ArrayList<String> lista = new ArrayList<String>();
            lista.add("frase que pode ser considerada muito longa");
            lista.add("frase curta");
            lista.add("frase com mais palavras");
    
            Collections.sort(lista, new Comparator<String>() {
    
                @Override
                public int compare(String o1, String o2) {
                    // TODO Auto-generated method stub
                    return o1.length() - o2.length();
                }
            });
    
            for (int i = 0; i < lista.size(); i++) {
                System.out.print(lista.get(i));
                System.out.println(" - tamanho: " + lista.get(i).length());
    
            }
        }
    }
    

    See working in ideone

        
    07.10.2018 / 02:48