I wanted to know the difference between sort function and ascending function in d3.
I am looking for a way to rearrange the data in my table in ascending order of the column selected.
Thanks.
Array.sort() will sort the values alphabetically in ascending order. Array.sort(d3.ascending) will sort the values naturally in ascending order. The difference can be seen when you are sorting a list of numbers.
var a = [3,26,1,7];
console.log(a.sort());
// prints [1,26,3,7]
console.log(a.sort(d3.ascending));
// prints [1,3,7,26]
For additional information on how sort works, see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort.
Related
I have a spreadsheet (here's a copy) with the following (headered) columns:
A: Indices for a list of groceries;
B: Names for the groceries to be indexed by column A;
C: Check column with "x" for inactive items in column B, empty otherwise;
D: Sorting indices that I want to apply to column B;
Currently, I am getting the sorted AND filtered result with this formula:
=SORT(FILTER(B2:B; C2:C = ""); FILTER(D2:D; C2:C = ""); TRUE)
The problem is that I need to apply the filter two times: one for the items and one for the indices, otherwise I get a mismatch between elements for the Sort function.
I feel that this doesn't scale well since it creates duplication.
Is there a way to get the same results with a simpler formula or another arrangement of columns?
=SORT(FILTER({Itens!B2:B\Itens!G2:G}; Itens!D2:D=""))
=SORT(FILTER({Itens!B2:B\Itens!G2:G}; Itens!D2:D="");2;1)
or maybe: =SORT(FILTER(Itens!B2:B; Itens!D2:D="");2;1)
I am using TreeMap to as I want to store sorted keys. I have also passed the comparator to sort the order. Now, I want to retrieve the 2nd key form the map. How do I go about doing it. The TreeMap is as given below :
private TreeMap<Coupon, LineItem> couponVsDiscountLine = new TreeMap<>((c1, c2) -> c1.weight().compareTo(c2.weight()));
Getting the sorted keys from TreeMap :
TreeSet<Coupon> coupons = (TreeSet<Coupon>) couponVsDiscountLine.keySet();
There is no method in TreeSet to get(index) as the elements in TreeSet are not indexed.
Other question, which Set does keySet() method of TreeMap return? How does TreeMap store the keys internally?
I read in some post that the TreeMap or TreeSet does not maintain the order if any modifications is done on that. Does it mean that retrieval of element may not give the elements in the order specified in the comparator?
Importing this dataset as a table:
https://data.cityofnewyork.us/Housing-Development/Registration-Contacts/feu5-w2e2#revert
I use the following query to perform an aggregation and then attempt to sort in descending order based on the reduction field. My intention is sort based on the count of that field or to have the aggregation create a second field called count and sort the grouping results in descending order of the reduction array count or length. How can this be done in rethinkdb?
query:
r.table("contacts").filter({"Type": "Agent","ContactDescription" : "CONDO"}).hasFields("CorporationName").group("CorporationName").ungroup().orderBy(r.desc('reduction'))
I don't quite understand what you're going for, but does this do what you want? If not, what do you want to be different in the output?
r.table("contacts")
.filter({"Type": "Agent","ContactDescription" : "CONDO"})
.hasFields("CorporationName")
.group("CorporationName")
.ungroup()
.merge(function(row){ return {count: row('reduction').count()}; })
.orderBy(r.desc('count'))
You are almost there:
r.table("contacts").filter({"Type": "Agent","ContactDescription" : "CONDO"}).hasFields("CorporationName").group("CorporationName").count().ungroup().orderBy(r.desc('reduction'))
See that .count()? That is a map-reduce operation to get the count of each group.
I haven't tested the query on your dataset. Please comment in case you had problems with it.
EDIT:
If you want to add a count field and preserve the original document, you need to use map and reduce. In your case, it should be something like:
r.table("contacts").filter({"Type": "Agent","ContactDescription" : "CONDO"})
.hasFields("CorporationName")
.group("CorporationName")
.map(r.row.merge({count:1}))
.reduce(function(left, right){
return {
count: left('count').add(right('count')),
<YOUR_OTHER_FIELDS>: left('<YOUR_OTHER_FIELDS>'),
...
};
})
.ungroup().orderBy(r.desc(r.row('reduction')('count')))
EDIT:
I am not sure if this can do the trick, but it is worth a try:
.reduce(function(left, right){
return left.merge({count: left('count').add(right('count'))})
})
I have the following code in web2py. I am trying to retrieve how many types of items I have in the table and a count of how many of each of them there are.
count = db.table.field1.count()
rows=db((some criteria).select(db.table.field2, count, groupby=db.table.field2)
print rows
The print of this is:
table.field2, COUNT(table.field1)
4,3
6,4
9,2
Now I would like to sort from high to low by the count field, so the outcome would be:
6,4
4,3
9,2
What's the best way to do it? rows=rows.sort(lambda row: row.COUNT(table.field1)) did not work for me.
Instead of row.COUNT(table.field1), use row['COUNT(table.field1)'] or just row[count] (see here).
Note, you can also have the database do the sorting using the orderby argument:
rows = db(query).select(db.table.field2, count,
groupby=db.table.field2, orderby=count)
And for descending order: orderby=~count
for $i in cts:search(fn:collection()/article, $query)
let $snippet :=
search:snippet( $i,.....)
order by if ($randomize) then ()
else if($sort-by = "ascending") then
xs:date($i/date_posted), cts:score($i)
else xs:date($i/date_posted) descending, cts:score($i) descending
return
element{"article"}
{ .....
.....
.....
}
Problem:
In above x-query statement, the order by clause has a condition that if($randomize) then () else the output provided will be in descending manner.
I
have a requirement that according the sort-type provided by user I want to pull out the results in ascending or descending manner.
But for the above written code, my x-query statements won't validate.
Thanks in Advance,
~Prashant
Unfortunately sort specifications in XQuery are static, even more so than in XSLT. If you want to switch dynamically between an ascending or descending sort, the only way to do it is to invert the sort key. That's easy for numbers - just use -(KEY) instead of (KEY). It's not too difficult for dates either - subtract the date from some fixed date in the future (well, any date actually). I can't think of any convenient way to do it for strings, but fortunately you don't seem to be using strings.