Skip to main content

APIzation: Replication Package

[Q#29001286][A#29001780] How can i get in desending order? please suggest

https://stackoverflow.com/q/29001286

I am trying to write a program where user provide the input, and output should be in descending order leaving the string in same position . eg: if input is 3,p,11,.,4,# output should be 11,p,4,.,3,#. I have tried this. What is wrong? please suggest

Answer

https://stackoverflow.com/a/29001780

Here is a simple solution based on bubble sort:

APIzation

public static void main(final String[] args) {

        final String input = args[0];

        final String[] tokens = input.split(",");

        for (int i = 0 ; i < tokens.length ; i++) {
            for (int j = i + 1 ; j < tokens.length ; j++) {
                String left = tokens[i];
                String right = tokens[j];
                if (isNumber(left) && isNumber(right)) {
                    int lNum = Integer.valueOf(left);
                    int rNum = Integer.valueOf(right);
                    if (lNum < rNum) {
                        swap(tokens, i, j);
                    }
                }
            }
        }

     }

    private static boolean isNumber(String s) {
        try {
            Integer.parseInt(s);
            return true;
        }
        catch (NumberFormatException nfe) {
            return false;
        }
    }

    private static void swap(final String[] arr, int i, int j) {
        final String tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
package com.stackoverflow.api;

public class Human29001780 {

  public static void sortInput() {
    String input = args[0];

    String[] tokens = input.split(",");

    for (int i = 0; i < tokens.length; i++) {
      for (int j = i + 1; j < tokens.length; j++) {
        String left = tokens[i];
        String right = tokens[j];
        if (isNumber(left) && isNumber(right)) {
          int lNum = Integer.valueOf(left);
          int rNum = Integer.valueOf(right);
          if (lNum < rNum) {
            swap(tokens, i, j);
          }
        }
      }
    }
  }

  private static boolean isNumber(String s) {
    try {
      Integer.parseInt(s);
      return true;
    } catch (NumberFormatException nfe) {
      return false;
    }
  }

  private static void swap(String[] arr, int i, int j) {
    String tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }
}

package com.stackoverflow.api;

/**
 * How can i get in desending order? please suggest
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/29001780">https://stackoverflow.com/a/29001780</a>
 */
public class APIzator29001780 {

  public static void get(String str1) {
    final String input = str1;
    final String[] tokens = input.split(",");
    for (int i = 0; i < tokens.length; i++) {
      for (int j = i + 1; j < tokens.length; j++) {
        String left = tokens[i];
        String right = tokens[j];
        if (isNumber(left) && isNumber(right)) {
          int lNum = Integer.valueOf(left);
          int rNum = Integer.valueOf(right);
          if (lNum < rNum) {
            swap(tokens, i, j);
          }
        }
      }
    }
  }

  private static boolean isNumber(String s) {
    try {
      Integer.parseInt(s);
      return true;
    } catch (NumberFormatException nfe) {
      return false;
    }
  }

  private static void swap(final String[] arr, int i, int j) {
    final String tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }
}