-5

I am new to this site and new to java so pleas help me out. If i have array of positive and negative numbers all are int, how to sort all the positive ones in a new array and all negative in a other new array.

  • 3
    Welcome to Stack Overflow! Please take the [tour](//stackoverflow.com/tour), have a look around, and read through the [help center](//stackoverflow.com/help), in particular [How do I ask a good question?](//stackoverflow.com/help/how-to-ask) and [What topics can I ask about here](//stackoverflow.com/help/on-topic)? – QBrute Jan 10 '18 at 12:14
  • 1
    Please see https://stackoverflow.com/help/mcve – Pratyay Jan 10 '18 at 12:15
  • 2
    Possible duplicate of [Sort an array in Java](https://stackoverflow.com/questions/8938235/sort-an-array-in-java) –  Jan 10 '18 at 12:21
  • 1
    Welcome to Stack Overflow! Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Jan 10 '18 at 12:39

3 Answers3

2

Asking a question in StackOverflow without any selfstudy on basics would attract downvotes. Kindly take time in doing your research. However to make you understand the way to solve I am providing you with a solution.

Step 1: You have an array of positive and negative values

Integer[] initialArray = new Integer[10];

//This contains the list of all values.

Step 2: Create two ArrayLists to save the negative and positive values in each of these.

ArrayList<Integer> positiveValues = new ArrayList<>();
ArrayList<Integer> negativeValues = new ArrayList<>();

Step 3: Iterate through initialArray and save the respective values in both lists.

for(int i =0; i< initialArray.length; i++) {
  if(initialArray[i] < 0) {
    negativeValues.add(initialArray[i]);
  } else {
    positiveValues.add(initialArray[i]);
  }
}

Step 4: Now sort the values

Collections.sort(negativeValues);
Collections.sort(positiveValues);

Step 5: If Required If you need in arrays instead of ArrayList,

Integer[] negativeArray = new Integer[negativeValues.size()];
Integer[] positiveArray = new Integer[positiveValues.size()];

And cast these to get the array

ChaitanyaAtkuri
  • 1,615
  • 9
  • 15
0

I actually think this is a pretty good question. Here is an implementation you can try using Java 8 streams.

Given an integer array full of mixed negative/positive numbers:

int[] a = new int[] { 5, 6, 7, 4, -2, 5, -9, 2 };

One solution could be as follows:

int[] pos = Arrays.stream(a).filter(i -> i >= 0).sorted().toArray();
int[] neg = Arrays.stream(a).filter(i -> i < 0).sorted().toArray();

The pos[] array will have all the positive numbers sorted, and neg[] all the negative numbers sorted. The only thing I really dislike is that a[] is traversed twice, but at least it is short.

Jaco Van Niekerk
  • 3,894
  • 2
  • 18
  • 41
-1

First seperate positive and negative numbers. I mean find size of positive numbers and create-set new positive numbers array. Then find size of negative numbers and create-set new negative numbers array.

Then just call sort method on both arrays.

Arrays.sort(array);
hellzone
  • 4,413
  • 18
  • 69
  • 118
  • 1
    Why was this voted down!? He also could have used Collections for this, but this is a legit answer, and seeing that Paojepablo wants us to do his homework, the length of that answer was also good. So why the downvote? – JayC667 Jan 10 '18 at 12:20
  • I need to make a logic that will move all positive values in new array and all negative in another new array – Paojepablo Jan 10 '18 at 12:20
  • @JayC667: The problem is that while the asker asks for a way to "sort" his array in reality he wants to seperate positive and negative numbers into two seperate arrays. – OH GOD SPIDERS Jan 10 '18 at 12:23
  • I was not talking about downvoting Paojepablo's question, but hellzone's answer. His answer was perfect... not too much to do Paojepablo's homework, enough to give an overview... – JayC667 Jan 10 '18 at 12:26
  • Bro i didnt and cant vote down on him coz i am new, and i dont see his answer voted down at all. Why not help and just flame. True its for homework but its just a prat i was stuck on coz i am new to this. Plz dont heat i write i am new and all... – Paojepablo Jan 10 '18 at 12:35