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.
-
3Welcome 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
-
1Please see https://stackoverflow.com/help/mcve – Pratyay Jan 10 '18 at 12:15
-
2Possible duplicate of [Sort an array in Java](https://stackoverflow.com/questions/8938235/sort-an-array-in-java) – Jan 10 '18 at 12:21
-
1Welcome 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 Answers
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

- 1,672
- 11
- 15
-
1Not quite right... you cannot sort Collection classes with `java.util.Arrays`. You'd have to use `java.util.Collections` for that. – JayC667 Jan 10 '18 at 12:29
-
That is is. I do rly close to this. TY m8 and sorry my first time here. Cheers – Paojepablo Jan 10 '18 at 12:30
-
-
`final Integer[] positives = positiveValues.toArray(new Integer[0]);`is much easier^^ – JayC667 Jan 10 '18 at 12:31
-
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.

- 4,180
- 2
- 21
- 48
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);

- 5,393
- 25
- 82
- 148
-
1Why 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