Java Arrays.sort Examples

Java Arrays.sort - 30 examples found. These are the top rated real world Java examples of java.util.Arrays.sort extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  public static void main(String[] args) {
    try {
      BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

      int testCases = Integer.parseInt(consoleReader.readLine());
      for (int i = 0; i < testCases; i++) {
        int pairs = Integer.parseInt(consoleReader.readLine());
        String[] men = consoleReader.readLine().split(" ");
        String[] women = consoleReader.readLine().split(" ");

        int[] menRatings = new int[pairs];
        int[] womenRatings = new int[pairs];
        for (int j = 0; j < pairs; j++) {
          menRatings[j] = Integer.parseInt(men[j]);
          womenRatings[j] = Integer.parseInt(women[j]);
        }

        Arrays.sort(menRatings);
        Arrays.sort(womenRatings);

        int sum = 0;

        for (int k = pairs - 1; k >= 0; k--) {
          sum += womenRatings[k] * menRatings[k];
        }

        System.out.println(sum);
      }

    } catch (Exception exc) {

    }
  }
Example #2
0
  @Override
  public List<File> loadInBackground() {

    ArrayList<File> list = new ArrayList<File>();

    // Current directory File instance
    final File pathDir = new File(mPath);
    list.add(pathDir.getParentFile());

    // List file in this directory with the directory filter
    final File[] dirs = pathDir.listFiles(FileUtils.sDirFilter);
    if (dirs != null) {
      // Sort the folders alphabetically
      Arrays.sort(dirs, FileUtils.sComparator);
      // Add each folder to the File list for the list adapter
      for (File dir : dirs) list.add(dir);
    }

    // List file in this directory with the file filter
    final File[] files = pathDir.listFiles(FileUtils.sFileFilter);
    if (files != null) {
      // Sort the files alphabetically
      Arrays.sort(files, FileUtils.sComparator);
      // Add each file to the File list for the list adapter
      for (File file : files) list.add(file);
    }

    return list;
  }
  public static void sort(int m[][]) {
    String[] nums = new String[m.length]; // creating string array
    Arrays.fill(nums, ""); // filling array with "" to avoid it beeing null
    for (int i = 0; i < m.length; i++) {
      for (int j = 0; j < m[i].length; j++) {
        nums[i] += String.valueOf(m[i][j]) + " "; // converting rows of
        // our 2D array into
        // string array

      }
    }
    Arrays.sort(nums); // sorting the string array
    String[][] secondary = new String[m.length][m[0].length];
    // converting back to 2D String array so we and performing secondary
    // sorting(sorting of the rows)
    for (int i = 0; i < secondary.length; i++) {
      secondary[i] = nums[i].split(" ");
      Arrays.sort(secondary[i]);
    }
    // printing out the new sorted 2D array
    for (String[] e : secondary) {
      for (String i : e) {
        System.out.print(i + " ");
      }
      System.out.println();
    }
  }
Example #4
0
  // Write a method that determines if two strings are anagrams of each other. For example, “TEAM”
  // and “MEAT” would return true.
  public static boolean isAnagram(String a, String b) {
    // Remove whitespace using a regular expression
    a = a.replaceAll("\\s+", "");
    b = b.replaceAll("\\s+", "");

    // Change to uppercase to reduce amount of possible characters
    a = a.toUpperCase();
    b = b.toUpperCase();

    // Turn the string into char arrays
    char i[] = a.toCharArray();
    char j[] = b.toCharArray();

    // Sort the arrays
    Arrays.sort(i);
    Arrays.sort(j);

    // Now check for equality
    for (int c = 0; c < i.length; c++) {
      // Since these are sorted, the indicies should match up.
      if (i[c] != j[c]) {
        return false;
      } // if
    } // for

    // Else, return true
    return true;
  } // isAnagram
  private void assertProposedMethods(
      String finderName,
      String[] properties,
      String[] superProperties,
      String[] expectedMethodProposalNames)
      throws JavaModelException {
    String superName = null;
    if (superProperties != null) {
      superName = "SearchSuper";
      createDomainClass(
          "SearchSuper", createDomainTextWithSuper(null, null, true, superProperties));
    }
    String contents = createDomainTextWithSuper(finderName, superName, false, properties);
    // ensure this is executed in a static context
    GroovyCompilationUnit unit = createDomainClass("Search", contents);
    DynamicFinderValidator validator =
        new FinderValidatorFactory()
            .createValidator(GrailsWorkspaceCore.get().create(project).getDomainClass(unit));
    List<AnnotatedNode> proposed = validator.findProposals(finderName);
    String[] actualMethodProposalNames = getProposalNames(proposed);
    Arrays.sort(expectedMethodProposalNames);
    Arrays.sort(actualMethodProposalNames);

    assertTrue(
        "Actual and expected proposals not the same.\nExpected: "
            + Arrays.toString(expectedMethodProposalNames)
            + "\nActual: "
            + Arrays.toString(actualMethodProposalNames),
        testSameElements(expectedMethodProposalNames, actualMethodProposalNames));
  }
  public static void main(String args[]) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int t = Integer.parseInt(br.readLine());
    String[] s1 = br.readLine().split(" ");
    String[] s2 = br.readLine().split(" ");
    int[] a1 = new int[t];
    int[] a2 = new int[t];
    for (int i = 0; i < t; i++) {
      a1[i] = Integer.parseInt(s1[i]);
      a2[i] = Integer.parseInt(s2[i]);
    }
    Arrays.sort(a1);
    Arrays.sort(a2);
    int i = 0;
    int cnt = 0;
    for (int k = t - 1; k >= 1; k--) {
      if (a2[k] == a1[i]) cnt += 50;
      else {
        if (a2[k] < a1[i]) cnt += 100;
      }
      i++;
    }
    System.out.println(cnt);
  }