Home > Net >  add an element to the last index when ordering alphabetically
add an element to the last index when ordering alphabetically

Time:04-13

Im creating a method which re arrange an array in alphabetical order. I want to add the element "EMPTY" after arranging the other elements alphabetically but every time I skip the loop it prints

public static void main(String[] args)
{
    int number;
    String str;
    Scanner sc1 = new Scanner(System.in);

    String [] items = {"hi","EMPTY","ahi"};

    for(int a = 0; a < items.length; a  )
    {
        if (items[a].equals("EMPTY")){
            continue;
        }
        for(int b = a   1; b < items.length; b  )
        {
            // java alphabetical sort


             if(items[a].compareTo(items[b]) > 0)
            {
                str = items[a];
                items[a] = items[b];
                items[b] = str;
            }
        }
    }
    System.out.println("After sorting names in an alphabetical order: ");
    for(int a = 0; a < items.length - 1; a  )
    {
        System.out.println(items[a]   ", ");
    }
    System.out.print(items[items.length - 1]);
}

CodePudding user response:

You can use Arrays.sort with a custom compare function that always puts "EMPTY" last.

public static void main (String[] args) throws java.lang.Exception
{
    String [] items = {"hi","EMPTY","ahi"};
    Arrays.sort(items, (s1, s2) -> {
                            if (s1.equals("EMPTY")) return 1;
                            else if (s2.equals("EMPTY")) return -1;
                            else return s1.compareTo(s2);
                        });
                    
    System.out.println("After sorting names in an alphabetical order: ");
    System.out.println(String.join(", ", items));
}

If you can't use Arrays.sort, you can re-write your code as

for(int a = 0; a < items.length; a  )
{
    for(int b = a   1; b < items.length; b  )
    {
        // EMPTY is before - swap
        if (items[a].equals("EMPTY")) swap(items, a, b);
        // EMPTY is after - leave it
        else if (items[b].equals("EMPTY")) continue;
        // Neither is EMPTY - regular compare
        else if (items[a].compareTo(items[b]) > 0) swap(items, a, b);
    }
}

Or shorten that to:

for(int a = 0; a < items.length; a  )
{
    for(int b = a   1; b < items.length; b  )
    {
        if ((items[a].equals("EMPTY")) || (!items[b].equals("EMPTY") && items[a].compareTo(items[b]) > 0)) {
            swap(items, a, b);
        }
    }
}

Lastly, to use a comparator in your code, first create the compare function:

public static int compare(String a, String b) {
    if (a.equals("EMPTY")) return 1;
    else if (b.equals("EMPTY")) return -1;
    else return a.compareTo(b);
}

Then use it with

for(int a = 0; a < items.length; a  )
{
    for(int b = a   1; b < items.length; b  )
    {
        if (compare(items[a], items[b]) > 0) {
            swap(items, a, b);
        }
    }
}

Note: I use a swap helper function to keep things tidy. It is simply:

public static void swap(String[] array, int a, int b) {
    String temp = array[a];
    array[a] = array[b];
    array[b] = temp;
}

CodePudding user response:

You can just make sure that EMPTY is "smaller" than everything else.

public static void main(String[] args)
{
    int number;
    String str;
    Scanner sc1 = new Scanner(System.in);

    String [] items = {"hi","EMPTY","ahi"};

    for(int a = 0; a < items.length; a  )
    {
        for(int b = a   1; b < items.length; b  )
        {
            // java alphabetical sort


             if((items[a].equals("EMPTY")) || (items[a].compareTo(items[b]) > 0))
            {
                str = items[a];
                items[a] = items[b];
                items[b] = str;
            }
        }
    }
    System.out.println("After sorting names in an alphabetical order: ");
    for(int a = 0; a < items.length - 1; a  )
    {
        System.out.println(items[a]   ", ");
    }
    System.out.print(items[items.length - 1]);
}
  • Related