0

Actually I want to sort Array List of objects. I am using Comparable interface for this purpose. It is completely working But the problem is that when I am sorting is is giving these two problems.

  1. All name who have 1st letter capital are coming together on the top and all name who have 1st letter small are coming together on the bottom.

  2. All the sorted Capital letters word are coming together after that all the small letter words are coming at bottom together.

Here is my bean

MyShares.java

   public class Myshares implements Comparable<Myshares> {

        int id, parent;
        String name, path, type, shared_with, shared_or_not, upload_request;

        public int getParent() {
            return parent;
        }

        public void setParent(int parent) {
            this.parent = parent;
        }

        public String getUpload_request() {
            return upload_request;
        }

        public void setUpload_request(String upload_request) {
            this.upload_request = upload_request;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getShared_with() {
            return shared_with;
        }

        public void setShared_with(String shared_with) {
            this.shared_with = shared_with;
        }

        public String getShared_or_not() {
            return shared_or_not;
        }

        public void setShared_or_not(String shared_or_not) {
            this.shared_or_not = shared_or_not;
        }

        @Override
        public int compareTo(Myshares another) {

            return this.name.compareTo(another.getName());
        }

    }

This is the output

enter image description here

I think it is based on ASCII code. I want a complete sorted list. Please take a look.


variable should visible to only present function in xtext

My output file:

reg1 {
field a field b
}

reg2 {
field c field d
}

FOREACH ( X IN reg1 ) {
X . a
}

FOREACH ( Y IN reg2 ) {

Now after this if I do ctrl+space I will expect only Y should pop-up. But X and Y both are getting pop-up. How to restrict X to first FOREACH loop? out of first FOREACH loop X and its value should be erased. Please anyone suggest me how to implement this?

The related grammar rule is:

Model:
entities+=Entity+
uses+=Use+
;
Entity:
name=ID "{"
attributes+=Attr+
"}"
;
Attr:
"field" name=ID
;
Use:
"FOREACH" var=conds "{"
varref=[cond] "." attr=[Attr]
"}"
;
conds:
"(" cond1=cond ")"
| "," cond2=cond
;
cond:
name=ID "IN" entity=[Entity]
;

Scope provider :

public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
IScope scope_Use_attr(Use ctx , EReference ref) {
return Scopes.scopeFor(ctx.getVarref().getEntity().getAttributes());
}
}
4

5 回答 5

6

如果您希望进行不区分大小写的排序,我建议您更改您的compareTo()方法以改为使用以下compareToIgnoreCase()方法String- 即:

public int compareTo(Myshares another) {
    return this.name.compareToIgnoreCase(another.getName());
}
于 2013-04-24T07:14:20.590 回答
5

改变

@Override
public int compareTo(Myshares another) {
    return this.name.compareTo(another.getName());
}

@Override
public int compareTo(Myshares another) {
    return String.CASE_INSENSITIVE_ORDER.compare(this.name, another.getName());
}

或使用nullPainter发布的更具可读性且非常好的解决方案。

于 2013-04-24T07:14:25.520 回答
2

我给了你一些我写的现有比较器的来源,它可以进行完整的字母数字排序,并且可以处理名称中的数字,如 10、1 等

要使用它,你可以这样做: Collections.sort(myList,comparator);

另一种方法是在您的比较方法中将两边都小写。但只要你有数字或符号,它就会被丢弃,所以我会亲自使用比较器。

使用源代码检查 GIST:https ://gist.github.com/gondor/9328de5fa0cce130bc3b

于 2013-04-24T07:20:14.363 回答
2

你应该调整你的compareTo()方法

return this.name.toLowerCase().compareTo(another.getName().toLowerCase());
于 2013-04-24T07:15:04.970 回答
2

将 compareTo 方法替换为:

public int compareTo(Myshares another) {
  return this.name.compareToIgnoreCase(another.getName());
}
于 2013-04-24T07:15:53.263 回答