Home Demos Forum Research Guide
Viewing topic in forum 'general'
back to topic list

Data Relational Comparison
Is there any way i can compare two data types using relational operators like <, or >?
by enesi on 2015-06-09 12:43:32 (3 replies)

Hello there,

Well, as far as I know you can definitely compare primitive data types like byte, integers and even strings. For example:


int a = 0;
int b = 3;

if (a > b) {
    out.println("a greater than b");
} else {
    out.println("a not greater than b");
}

// or...
char a[] = "testing";
char b[] = "testing";

if (a == b) {
    out.println("a matches b");
}
 


Also, it's worth adding that you can define any data type and compare the inner variables one by one. For example:


data ListItem{
    Data item;
    ListItem next;
    ListItem prev;
}

ListItem lw = list;
if (lw.item === item) {...}
 


Did that help? I could definitely be more helpful if you could provide more information on what you're trying to achieve.

- Roberto
by roberto on 2015-06-09 14:29:45
I am trying to create a sorted linked list. As the name implies the position of a new item is determined by comparing it with existing items. The Sorted Linked List items are of type "data". I am trying to figure out how to do comparison with data.
by enesi on 2015-06-10 12:43:49
That sounds pretty interesting. Unfortunately, I don't think you can compare Data-typed variables. Data can be integers, strings or even another Data. So, since you can't predict the type the variable values are going to be, you can't really compare them.

A simple solution for this would be to create a key inside of your list data type. Such as:


data ListItem {
 data item;
 int key;
}
 


Then you can use the key value to sort the list out. This is a rather simplistic solution. A more interesting one would be to take advantage of Dana's adaptation features. You could implement different versions of the linked list (one to sort integers, another to sort strings, etc.), then you just need to figure out the type being assigned to the list items and decide (at runtime or not) which linked list component to use. I believe we can come up with very interesting and creative approaches by exploring adaptation. I'm just throwing some ideas out there.

- Roberto


by roberto on 2015-06-10 14:14:43
© Francis Research Ltd. 2018