Class: Ferret::Search::Sort
Summary
A Sort object is used to combine and apply a list of SortFields. The SortFields are applied in the order they are added to the SortObject.
Example
Here is how you would create a Sort object that sorts first by rating and then by title;
sf_rating = SortField.new(:rating, :type => :float, :reverse => true) sf_title = SortField.new(:title, :type => :string) sort = Sort.new([sf_rating, sf_title])
Remember that the :type parameter for SortField is set to :auto be default be I strongly recommend you specify a :type value.
Constants
| Name | Value |
|---|---|
| RELEVANCE | frt_sort_init(0, NULL, frt_sort_alloc(cSort)) |
| INDEX_ORDER | frt_sort_init(1, &oSORT_FIELD_DOC, frt_sort_alloc(cSort)) |
Public Class Methods
Sort.new(sort_fields = [SortField::SCORE, SortField::DOC_ID], reverse = false) → Sort
Create a new Sort object. If reverse is true, all sort_fields will be reversed so if any of them are already reversed the will be turned back to their natural order again. By default
/*
* call-seq:
* Sort.new(sort_fields = [SortField::SCORE, SortField::DOC_ID], reverse = false) -> Sort
*
* Create a new Sort object. If +reverse+ is true, all sort_fields will be
* reversed so if any of them are already reversed the will be turned back
* to their natural order again. By default
*/
static VALUE
frt_sort_init(int argc, VALUE *argv, VALUE self)
{
int i;
VALUE rfields, rreverse;
bool reverse = false;
bool has_sfd = false;
GET_SORT();
switch (rb_scan_args(argc, argv, "02", &rfields, &rreverse)) {
case 2: reverse = RTEST(rreverse);
case 1:
if (TYPE(rfields) == T_ARRAY) {
int i;
for (i = 0; i < RARRAY(rfields)->len; i++) {
frt_sort_add(sort, RARRAY(rfields)->ptr[i], reverse);
}
} else {
frt_sort_add(sort, rfields, reverse);
}
for (i = 0; i < sort->size; i++) {
if (sort->sort_fields[i] == &SORT_FIELD_DOC) has_sfd = true;
}
if (!has_sfd) {
sort_add_sort_field(sort, (SortField *)&SORT_FIELD_DOC);
}
break;
case 0:
sort_add_sort_field(sort, (SortField *)&SORT_FIELD_SCORE);
sort_add_sort_field(sort, (SortField *)&SORT_FIELD_DOC);
}
return self;
}Public Instance Methods
sort.fields → Array
Returns an array of the SortFields held by the Sort object.
/*
* call-seq:
* sort.fields -> Array
*
* Returns an array of the SortFields held by the Sort object.
*/
static VALUE
frt_sort_get_fields(VALUE self)
{
GET_SORT();
VALUE rfields = rb_ary_new2(sort->size);
int i;
for (i = 0; i < sort->size; i++) {
rb_ary_store(rfields, i, object_get(sort->sort_fields[i]));
}
return rfields;
}
sort.to_s → string
Returns a human readable string representing the sort object.
/*
* call-seq:
* sort.to_s -> string
*
* Returns a human readable string representing the sort object.
*/
static VALUE
frt_sort_to_s(VALUE self)
{
GET_SORT();
char *str = sort_to_s(sort);
VALUE rstr = rb_str_new2(str);
free(str);
return rstr;
}