I have following data in string(comma format) (Name,Mark)
A,20,B,10,C,30
I want to convert into Descending order like :
C,30,A,29,B,10
Please help me how can i implement in android ?
Here is code what i have prepared....
public class custom_sort {
public String name;
public int mark;
public custom_sort(String a, int b) {
// TODO Auto-generated constructor stub
name = a;
mark = b;
}
void setname(String s)
{
name=s;
}
void setmark(int s)
{
mark = s;
}
String getname()
{
return(name);
}
int getmark()
{
return(mark);
}
}
Thanks in Advance,
There are many ways of doing this. I see that you have already made a class: custom_sort. We can use this to sort it, if we just make it comparable. We do this by implementing the Comparable interface.
public class custom_sort implements Comparable{
Then all you need to do is implement the one required method:
#Override
public int compareTo(custom_sort cs) {
/*
This method should return 0 if the two objects are equal,
1 if this is biggest
and -1 if cs is biggest */
}
Then you can put all the custom_sorts in an List and just do Arrays.sort(yourArray).
You could also check out this post Android sort array
Use ArrayList rather than array of string.
This might not be the best solution but it works.
Initialize the ArrayList
ArrayList<String> names = new ArrayList<>();
ArrayList<Integer> scores = new ArrayList<>();
Fill data inside the ArrayList
names.add("A");
scores.add(20);
....
Now sort them
private void sortScoreAndName() {
for (int i = 0; i < scores.size(); i++) {
for (int j = 0; j < i; j++) {
if (scores.get(i) > scores.get(j))
swap(i, j);
}
}
}
private void swap(int i, int j) {
int tempSco = scores.get(i);
String tempName = names.get(i);
scores.remove(i);
names.remove(i);
scores.add(i, scores.get(j));
names.add(i, names.get(j));
scores.remove(j);
names.remove(j);
scores.add(j, tempSco);
names.add(j, tempName);
}
Now your ArrayList is in descending order. You can get the corresponding data using.
names.get(poition);
scores.get(position);
try this:
import android.support.v4.util.Pair;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
String[] values = {"A", "20", "B", "10", "C", "30"};
List<Pair> pairs = new ArrayList<>();
for (int i = 0; i < values.length; i += 2) {
pairs.add(new Pair<>(values[i], Integer.parseInt(values[i + 1])));
}
Collections.sort(pairs, new Comparator<Pair>() {
#Override
public int compare(Pair lhs, Pair rhs) {
return ((Integer) rhs.second).compareTo((Integer) lhs.second);
}
});
The List pairs is now sorted like you wish. Check out the Pair class i used http://developer.android.com/reference/android/util/Pair.html
And if you want to convert it back following code will help you:
String[] newValues = new String[values.length];
int i = 0;
for (Pair pair : pairs) {
newValues[i] = (String) pair.first;
newValues[i + 1] = Integer.toString((Integer) pair.second);
i += 2;
}
Related
can anyone please explain me how comaprator internally works with ArrayList?
My code is working but I am not getting how internally things have been processed.
**program definition:**Sort name in descending order of there score,if two scores are equal then display names in alphabetic order.
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
Player[] player = new Player[n];
Checker1 checker1 = new Checker1();
for(int i = 0; i < n; i++){
player[i] = new Player(scan.next(), scan.nextInt());
}
scan.close();
Arrays.sort(player, checker1);
for(int i = 0; i < player.length; i++){
System.out.printf("%s %s\n", player[i].name, player[i].score);
}
}
}
class Checker1 implements Comparator<Player> {
#Override
public int compare(Player p1, Player p2) {
String s1=(String)p1.name;
String s2=(String)p2.name;
// TODO Auto-generated method stub
System.out.println(p1.score+"..."+p2.score);
if(p1.score>p2.score){
return -1;
}
else if(p1.score==p2.score){
return s1.compareTo(s2);
}
return 0;
}
}
In order to ensure a consistent result when comparing pairs of elements a, b and b, a the comparison operations need to be consistent, here if p1.score < p2.score you return 0. But, you return -1 if it is greater. That violates the general contract; Java, in many cases, expects to be able to compare the elements in either order. Better to use Integer.compare(int, int) and return the result of comparing names only when that is 0. Like,
#Override
public int compare(Player p1, Player p2) {
System.out.println(p1.score + "..." + p2.score);
int c = Integer.compare(p1.score, p2.score);
if (c == 0) {
return p1.name.compareTo(p2.name);
}
return c;
}
I have an arraylist that looks like this:
public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();
I store groups of 2 persons in a pair. For example:
[Person1, Person2]
[Person3, Person4]
The algorithm I use right now still makes duplicates, I've tried out hashmaps and iterating through them with for loop but they just give me back the original list.
This is the code:
package com.company;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class createGroups
{
public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();
public static void main(String[] args){
//Define names
String[] names = {"Person1", "Person2", "Person3", "Person4"};
try
{
//Create combinations. In a try catch because of the saveFile method.
combination(names, 0, 2);
//Print all the pairs in the Arraylist x
printPairs();
} catch (IOException e)
{
e.printStackTrace();
}
}
static void combination(String[] data, int offset, int group_size) throws IOException
{
if(offset >= data.length)
{
//Create new Arraylist called foo
ArrayList<String[]> foo = new ArrayList<>();
//Create a pair of 2 (data.length = 4 / group_size = 2)
for(int i = 0; i < data.length / group_size; i++)
{
//Add the pair to foo.
foo.add(Arrays.copyOfRange(data, 2 * i, 2 * (i + 1)));
}
//Add foo to x
x.add(foo);
//saveFile(foo);
}
for(int i = offset; i < data.length; i++){
for(int j = i + 1; j < data.length; j++){
swap(data, offset, i);
swap(data, offset + 1, j);
combination(data, offset + group_size, group_size);
swap(data, offset + 1, j);
swap(data, offset, i);
}
}
}
public static void printPairs(){
//Print all pairs
for(ArrayList<String[]> q : x){
for(String[] s : q){
System.out.println(Arrays.toString(s));
}
System.out.println("\n");
}
}
private static void swap(String[] data, int a, int b){
//swap the data around.
String t = data[a];
data[a] = data[b];
data[b] = t;
}
}
The output right now is this:
Output
Every group of 4 names is a 'list' of pairs (Not really a list but that's what I call it)
And this is the desired output:
Desired output
But then you can see that the first and the last list of pairs are basically the same how do I change that in my combination method
The question:
How can I change my combination method so that it doesn't create duplicate groups.
And how can I make the list smaller (The desired output) when printing the created lists.
If I wasn't clear enough or if I didn't explain what I want very well, let me know. I'll try to make it clearer.
Create an object similar to this. It takes 4 strings (2 pairs). Puts the strings into array and sorts this array. That means any combination of strings you put in will be converted into one sorted combination, but the object internaly remembers which person is person1, person2, ...
private class TwoPairs {
private final String person1;
private final String person2;
private final String person3;
private final String person4;
private final String[] persons;
TwoPairs(String person1, String person2, String person3, String person4) {
this.person1 = person1;
this.person2 = person2;
this.person3 = person3;
this.person4 = person4;
persons = new String[4];
persons[0] = person1;
persons[1] = person2;
persons[2] = person3;
persons[3] = person4;
// if we sort array of persons it will convert
// any input combination into single (sorted) combination
Arrays.sort(persons); // sort on 4 objects should be fast
// hashCode and equals will be comparing this sorted array
// and ignore the actual order of inputs
}
// compute hashcode from sorted array
#Override
public int hashCode() {
return Arrays.hashCode(persons);
}
// objects with equal persons arrays are considered equal
#Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TwoPairs other = (TwoPairs) obj;
if (!Arrays.equals(persons, other.persons)) return false;
return true;
}
// add methods which you might need
// getters for individual persons
// String getPerson1() { return person1; }
// or perhaps pairs of persons
// String[] getPair1() { return new String[] {person1, person2}; }
// add sensible toString method if you need it
}
Your ArrayList x will change like this
ArrayList<TwoPairs> x = new ArrayList<TwoPairs>();
before adding new TwoPairs object into x check if this list already contains this object.
if (!x.contains(twoPairsObject)) {
x.add(twoPairsObject);
}
I have three ArrayLists. One of Strings - names, and two of Integers - score and picture numbers. I want to sort them simultaneously by the players scores (from highest to lowest). Now i use a simple bubble sort, but i think it will not be efficient when the Lists will be bigger.
This is my code:
public class MyBubbleSort {
public static void bubble_srt(List<Integer> score, List<String> name, List<Integer> pic) {
int n = score.size();
int k;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
k = i + 1;
if (score.get(i) < score.get(k)) {
swapNumbers(i, k, score, name, pic);
}
}
printNumbers(score);
}
}
private static void swapNumbers(int i, int j, List<Integer> score, List<String> name, List<Integer> pic) {
int temp;
temp = score.get(i);
score.set(i, score.get(j));
score.set(j, temp);
String s;
s = name.get(i);
name.set(i, name.get(j));
name.set(j, s);
int p;
p = pic.get(i);
pic.set(i, pic.get(j));
pic.set(j, p);
}
private static void printNumbers(List<Integer> input) {
for (int i = 0; i < input.size(); i++) {
System.out.print(input.get(i) + ", ");
}
System.out.print("\n");
}
}
Thanks!
Best way would be to create a class containing score, name and pic properties and have a single List of that class, which you sort using Collections.sort and a Comparator that compares two instances of your class according to the score property.
Bubble sort is in-efficient compared to other sorting algorithms (merge sort, quick sort), and there's no need to implement a sort algorithm yourself, since the standard Java packages already do that for you.
First create a PlayerInfo class as follows:
package test;
public class PlayerInfo {
private String name;
private Integer score;
private Integer pictureId;
public PlayerInfo(final String name, final Integer score, final Integer pictureId) {
this.name = name;
this.score = score;
this.pictureId = pictureId;
}
public String getName() {
return this.name;
}
public void setName(final String name) {
this.name = name;
}
public Integer getScore() {
return this.score;
}
public void setScore(final Integer score) {
this.score = score;
}
public Integer getPictureId() {
return this.pictureId;
}
public void setPictureId(final Integer pictureId) {
this.pictureId = pictureId;
}
#Override
public String toString() {
return this.name + ":" + this.score + ":" + this.pictureId;
}
}
Second create a PlayerInfo Comparator. Here we create a ScoreBasedComparator (as per your request, but you can create other comparators as well to fit your specific needs):
package test;
import java.util.Comparator;
public class ScoreBasedComparator implements Comparator<PlayerInfo> {
#Override
public int compare(final PlayerInfo playerInfo1, final PlayerInfo playerInfo2) {
return playerInfo1.getScore().compareTo(playerInfo2.getScore());
}
}
Finally, you can sort your List of PlayerInfo instances, using Collections.sort(<your collection>, <your comparator>) as follows:
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Runner {
public static void main(final String[] args) {
List<PlayerInfo> playerInfos = new ArrayList<PlayerInfo>();
playerInfos.add(new PlayerInfo("A", 123, 1));
playerInfos.add(new PlayerInfo("B", 1, 2));
playerInfos.add(new PlayerInfo("C", 23, 3));
playerInfos.add(new PlayerInfo("D", 300, 4));
Collections.sort(playerInfos, new ScoreBasedComparator());
System.out.println(Arrays.toString(playerInfos.toArray()));
}
}
Running this small program will output the following line:
[B:1:2, C:23:3, A:123:1, D:300:4]
As you can see your collection was unsorted at creation time but is printed sorted by score.
Hope this helps.
If the goal here is to sort three arrays according to one of the arrays, without resorting to combining the arrays into a common class, then a fourth array of indices, 0 to size-1 can be created, then the array of indices is sorted according to one of the arrays (using a built in sort and custom compare). Then all three arrays are reordered according to the array of sorted indices. I don't know if Java has a built in reorder function. C example to reorder 3 arrays, A,B,C according to sorted array of indices I, with time complexity of O(n) (linear, every store places a value in it's ordered position). I is reverted back to 0 to n-1.
// reorder A,B,C in place according to I
// tA,tB,tC are temps
for(i = 0; i < n; i++){
if(i != I[i]){
tA = A[i];
tB = B[i];
tC = C[i];
k = i;
while(i != (j = I[k])){
A[k] = A[j];
B[k] = B[j];
C[k] = C[j];
I[k] = k;
k = j;
}
A[k] = tA;
B[k] = tB;
C[k] = tC;
I[k] = k;
}
}
I'm learning about comparable and am implementing it in my Inventory class. However when I go to compile the code, the compiler gives an error.
InventoryItem.java uses unchecked or unsafe operations.
Can anyone please help me out. What is wrong with my code and what can I do to fix this issue. Thank you for your help in advance.
class InventoryItem implements Comparable<InventoryItem>
{
private String name;
private int uniqueItemID;
public InventoryItem()
{
name = " ";
uniqueItemID = 0;
}
public InventoryItem(String newName, int newItemID)
{
name = newName;
uniqueItemID = newItemID;
}
public InventoryItem(InventoryItem i)
{
name = i.name;
uniqueItemID = i.uniqueItemID;
}
public void setName(String newName)
{
name = newName;
}
public void setItemID(int newItemID)
{
uniqueItemID = newItemID;
}
public int getItemID()
{
return uniqueItemID;
}
public String getName()
{
return name;
}
public int compareTo(InventoryItem i)
{
int anotherUniqueID = i.getItemID();
return (this.uniqueItemID - anotherUniqueID);
}
public static void sort(Comparable[] a, int numberUsed)
{
int index, indexOfNextSmallest;
for(index = 0; index < numberUsed - 1; index++)
{
indexOfNextSmallest = indexOfSmallest(index, a, numberUsed);
interchange(index, indexOfNextSmallest, a);
}
}
private static int indexOfSmallest(int startIndex, Comparable[] a, int numberUsed)
{
Comparable min = a[startIndex];
int indexOfMin = startIndex;
int index;
for(index = startIndex + 1; index < numberUsed; index++)
{
if(a[index].compareTo(min) < 0)
{
min = a[index];
indexOfMin = index;
}
}
return indexOfMin;
}
private static void interchange(int i, int j, Comparable[] a)
{
Comparable temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
public class InventoryItemTester
{
public static void main(String [] args)
{
InventoryItem[] items = new InventoryItem[3];
items[0] = new InventoryItem("Pens", 2);
items[1] = new InventoryItem("Pencils", 3);
items[2] = new InventoryItem("Notebooks", 1);
System.out.println("Before sorting");
System.out.println(items[0]);
System.out.println(items[1]);
System.out.println(items[2]);
InventoryItem.sort(items, items.length);
System.out.println("After sorting");
System.out.println(items[0]);
System.out.println(items[1]);
System.out.println(items[2]);
}
}
At a guess I'd say this line is causing the issue (Your compiler tells you exactly which line is the problem, this might be useful to include in your next question):
private static int indexOfSmallest(int startIndex, Comparable[] a, int numberUsed)
{
Comparable min = a[startIndex];
int indexOfMin = startIndex;
int index;
for(index = startIndex + 1; index < numberUsed; index++)
{
here==========> if(a[index].compareTo(min) < 0)
{
You are calling compareTo with a InventoryItem where it is expecting an Object. You could add a #SuppressWarnings annotation which would make it go away :)
The basic idea of Comparable and Comparator is they apply a sorting order to an Object so that the standard JDK Collections objects can do all the hard work for you.
In your case your comparesTo method does the correct thing, however I'm not sure if this is good planning or good luck, so things to note:
InventoryItem.comparesTo method needs to evaluate the current instance to the provided instance and return an integer signifying the ordering, -1 means the instance (ie this) should be ordered before the argument, 0 means they are the same and 1 means the instance is after the argument. Something like this lets the JDK do all the hard work for you
public int compareTo(InventoryItem i)
{
return Integer.valueOf(this.uniqueItemID).compareTo(i.uniqueItemID);
}
In order to use Comparable all you really need to do is implement it and then use the standard JDK Collections classes to do all the heavy lifting for you, eg:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class InventoryItemTest
{
public static void main(String [] args)
{
List<InventoryItem> items = new ArrayList<InventoryItem>();
items.add(new InventoryItem("Pens", 2));
items.add(new InventoryItem("Pencils", 3));
items.add(new InventoryItem("Notebooks", 1));
System.out.println("Before sorting");
System.out.println(items);
Collections.sort(items);
System.out.println("After sorting");
System.out.println(items);
}
}
I realise this might not be as much fun as writing your own sorting algorithms but if you want to do that with no compiler warnings then you need to start looking at generics
I want to create Integer vector type Vector and to insert random 10 numbers.
Then I want to sort the Vector by using compareTo and to send two arguments type numbers to compare it.
but I miss something.
Many thanks for any help.
import java.util.*;
public class SortNumeric implements Comparable<SortNumeric>
{
private int ind =0;
public static void main(String args[])
{
Vector<Integer> vec = new Vector<>();
System.out.println("Befor sorting");
for (int index = 0; index < 10; index++)
{
int rand = (int)(1000*Math.random());
vec.add(rand);
System.out.println(rand);
}
Arrays.sort(vec);
System.out.println("After sorting");
for(Integer intnum : vec)
{
System.out.println(intnum);
}
}
public int getNextCompar(){
if (vec.hasNext() && this.ind < 5){
this.ind++;
return vec.next();
}else{return 0;}
}
public int compareTo(SortNumeric other)
{
return (int) (vec.next() - this.getNextCompar());
}
}
class sortVectors implements Comparator<Integer>{
#Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
if(o1<02){
return -1;
}else if(o1>o2)
return 1;
return 0;
}
}
public class sortVector{
public static void main(String[] args) {
// TODO Auto-generated method stub
Vector<Integer> vect = new Vector<Integer>();
System.out.println("Befor sorting");
for (int index = 0; index < 10; index++) {
int rand = (int) (1000 * Math.random());
vect.add(rand);
System.out.println(rand);
}
Collections.sort(vect,new sortVectors());
System.out.println("After Sorting");
for (Integer num : vect) {
System.out.println(num);
}
}
}
You don't need to implement Comparable for Integer, hence no compareTo is required. Also use Collections.sort instead of Arrays.sort. Here is your modified code example:
import java.util.Collections;
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<Integer> vec = new Vector<>();
System.out.println("Befor sorting");
for (int index = 0; index < 10; index++) {
int rand = (int) (1000 * Math.random());
vec.add(rand);
System.out.println(rand);
}
Collections.sort(vec);
System.out.println("After sorting");
for (Integer intnum : vec) {
System.out.println(intnum);
}
}
}
Check this SO question for more details:
What function can be used to sort a Vector?
If you want to sort an array in a specific order, you need to create a Comparator.
Comparable is used when you want to sort the objects of a particular class when used in a collection or array.
In your case, you need to use a Comparator.
class MyComparator extends Compartor
{
public int compare(Integer a, Integer b)
{
return a - b;
}
}
In your Main class, call Arrays.sort(vec, new MyComparator());