Skip to main content
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
added 4 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

How to proper compare Comparing a string that can hold a numeric value?

I am refactoring a part of big old system developed in Java. I came across a situation where we are migrating some data types, and there will be use of both types. For compatibility, as all the old part will remain using id as an int, I simply overloaded the constructor, and the old getter now parse a integer value.

The thing is, how is the proper way to compare this data? Does this way make any sense, or is there a simpler way to compare it, considering that id can be both, a number or a word.

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class SomeItem implements Comparable<SomeItem> {
    private String id;    

    public SomeItem(String id) {
        this.id = id;
    }

    public SomeItem(int no) {
        this(String.valueOf(no));
    }

    public String getId() {
        return id;
    }

    public int getNo() {
        return Integer.parseInt(id);
    }

    @Override
    public int compareTo(SomeItem that) {
        if (StringUtils.isNumeric(this.getId()) && StringUtils.isNumeric(that.getId())) {
            return this.getNo() - that.getNo();
        } else {
            return this.getId().compareTo(that.getId());
        }
    }
}

}

How to proper compare a string that can hold a numeric value?

I am refactoring a part of big old system developed in Java. I came across a situation where we are migrating some data types, and there will be use of both types. For compatibility, as all the old part will remain using id as an int, I simply overloaded the constructor, and the old getter now parse a integer value.

The thing is, how is the proper way to compare this data? Does this way make any sense, or is there a simpler way to compare it, considering that id can be both, a number or a word.

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class SomeItem implements Comparable<SomeItem> {
    private String id;    

    public SomeItem(String id) {
        this.id = id;
    }

    public SomeItem(int no) {
        this(String.valueOf(no));
    }

    public String getId() {
        return id;
    }

    public int getNo() {
        return Integer.parseInt(id);
    }

    @Override
    public int compareTo(SomeItem that) {
        if (StringUtils.isNumeric(this.getId()) && StringUtils.isNumeric(that.getId())) {
            return this.getNo() - that.getNo();
        } else {
            return this.getId().compareTo(that.getId());
        }
    }

}

Comparing a string that can hold a numeric value

I am refactoring a part of big old system developed in Java. I came across a situation where we are migrating some data types, and there will be use of both types. For compatibility, as all the old part will remain using id as an int, I simply overloaded the constructor, and the old getter now parse a integer value.

The thing is, how is the proper way to compare this data? Does this way make any sense, or is there a simpler way to compare it, considering that id can be both, a number or a word.

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class SomeItem implements Comparable<SomeItem> {
    private String id;    

    public SomeItem(String id) {
        this.id = id;
    }

    public SomeItem(int no) {
        this(String.valueOf(no));
    }

    public String getId() {
        return id;
    }

    public int getNo() {
        return Integer.parseInt(id);
    }

    @Override
    public int compareTo(SomeItem that) {
        if (StringUtils.isNumeric(this.getId()) && StringUtils.isNumeric(that.getId())) {
            return this.getNo() - that.getNo();
        } else {
            return this.getId().compareTo(that.getId());
        }
    }
}
Source Link

How to proper compare a string that can hold a numeric value?

I am refactoring a part of big old system developed in Java. I came across a situation where we are migrating some data types, and there will be use of both types. For compatibility, as all the old part will remain using id as an int, I simply overloaded the constructor, and the old getter now parse a integer value.

The thing is, how is the proper way to compare this data? Does this way make any sense, or is there a simpler way to compare it, considering that id can be both, a number or a word.

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class SomeItem implements Comparable<SomeItem> {
    private String id;    

    public SomeItem(String id) {
        this.id = id;
    }

    public SomeItem(int no) {
        this(String.valueOf(no));
    }

    public String getId() {
        return id;
    }

    public int getNo() {
        return Integer.parseInt(id);
    }

    @Override
    public int compareTo(SomeItem that) {
        if (StringUtils.isNumeric(this.getId()) && StringUtils.isNumeric(that.getId())) {
            return this.getNo() - that.getNo();
        } else {
            return this.getId().compareTo(that.getId());
        }
    }

}