DataTable not sorting numeric set of data - sorting

Sorting of numbers under unique column is not proper, it is sorting numbers perfectly when there are same no of digits but when there comes different no digits sorting is not proper (sorting as a string).
$('.table').dataTable({
"searching": false
});
$($('#DataTables_Table_0_wrapper .row')[0]).hide();

Related

Lodash sortBy sorts and splits between integers and floats

I'm using Lodash, trying to use sortBy.
For some reason, it sorts, but doesn't treat integers and floats equally.
I end up with this "sorted" result:
Displayed using Chart.js:
Why is this?
My code:
output = _(dataSet)
.sortBy(output, dimension)
.groupBy(dimension)
.map((objs, key) => ({
dimension: key,
measure: _.meanBy(objs, measure) }))
.value();
The _.groupBy() method creates an object with the value of the property you've selected. Since dimension is a numeric property, they are traversed by the numeric value for integers, and as strings for floats (see this article).
The solution is to sort by the actual numeric value, after you convert the grouping object back to an array:
output = _(dataSet)
.groupBy(dimension)
.map((objs, key) => ({
dimension: _.head(objs, 'dimension'),
measure: _.meanBy(objs, measure) }))
.sortBy(output, dimension) // <- sort here
.value();

Custom data format in OBIEE - Showing decimal when double, no decimal when integer

I have measure column, which I am using for pivoting, and I have also used New calculated items. Now the new calculated item is to return data in double format,which is percentage, but the other results is to return data in integer. If the data format of the column is decimal then the measure column, which are integers would show data with decimals (so 2 becomes 2.00), and if i keep it integer then decimals from the percentage column would be removed (so 45.28% becomes 45%).
Can the data format of the column be changed such that when there are decimal, then decimals are returned and when whole numbers, whole numbers are returned (without the .00s)?
Expected Result
A B (A/B)*100
2 6 33.33
Note that A and B are coming from the same column, and the (A/B)*100 is my New Calculated Item.
Criteria tab / Properties / Data format and select "up to 2" in decimal places. This option excludes the ".00" for integer values.

Lua table.sort issues

So, having some issues getting this table to sort correctly.
Basically, table.sort thinks 10 == 1, 20 == 2, and so on. I'll post my sort code below, but I'm not sure if that has anything to do with it. Is this just an inherent issue with the table.sort algorithm in Lua?
if svKey == "q" and metalMatch == true then
table.sort(vSort.metals, function(oneMQ, twoMQ)
return oneMQ.metalQ > twoMQ.metalQ
end)
end
Values stored in vSort.metals.metalQ are strings anywhere from 1 to 3 digits long. Is there a way to make table.sort differentiate between single-, double-, and triple-digit values?
The order operators work as follows. If both arguments are numbers, then they are compared as such. Otherwise, if both arguments are strings, then their values are compared according to the current locale. You can set the locale. Strings are compared lexicographically, which is generally character by character with shorter strings before longer strings.
If you want a numeric sort, then use, well, a numeric type. This might work:
function(oneMQ, twoMQ)
return tonumber(oneMQ.metalQ) > tonumber(twoMQ.metalQ)
end
It assumes that all the metalQ values are numeric. If not, coerce to a default or provide a fallback sort order in your sort expression for non-numeric values.

Sort numbers with letters numerically and alphabetically

I have a Database on my website with a Long List of #'s (Product #'s) all containing letters (Exp. TC-345, TC-234 or HC-236W 123-234-PWD...)
Can we numerically and alphabetically sort the #'s on the website?
Currently we store it alphabetically, so the order is (10-PDW, 100-PDW, 110-PDW 2-PDW)
We would like to change it into (2-PDW, 10-PDW, 100-PDW, 110-PDW)
My developers say "The colorway number can never be sorted numerically. We would need to add another numeric field to the database for all the colorways and then sort that field numerically. Right now those numbers are in alphabetical order."
How do you sort a numbers with letters? We'd like to avoid adding a Numeric Field - that's just extra work. Is there any new technique out to do this?
It is possible. One way would be using a function for weighting strings, which gives far more weight for numbers than letters. Something like this:
letbers = ["10-PDW", "100-PDW", "110-PDW", "2-PDW"]
def weight(letber):
if letber == "":
return 0
n = ord(letber[-1])
if letber[-1] in "0123456789":
n *= 256^6 # 6 because maximum key length is 6
return 256*n + weight(letber[:-1])
print sorted(letbers, key = weight)
If you add the following transformation function (in scala), then you can alphabetically and numerically sort all strings:
def transformed(s: String): String = {
s.replaceAll("""(?<=[^\d]|^)(\d)(?=[^\d]|$)""","""000$1""")
.replaceAll("""(?<=[^\d]|^)(\d\d)(?=[^\d]|$)""","""00$1""")
.replaceAll("""(?<=[^\d]|^)(\d\d\d)(?=[^\d]|$)""","""0$1""")
}
Basically it replaces every number occurence by a fixed width integer, so that the alphabetical sorting equals the numerical sorting in that case.
Test on your input:
> val s = List("10-PDW", "100-PDW", "110-PDW", "2-PDW")
> s.sortBy(transformed)
res2: List[String] = List(2-PDW, 10-PDW, 100-PDW, 110-PDW)
This works only if you are sure that all numbers are below 9999. If you have more digits, then you should consider to expand the function or do something else.

Can't sort table with associative indexes

Why I can't use table.sort to sort tables with associative indexes?
In general, Lua tables are pure associative arrays. There is no "natural" order other than the as a side effect of the particular hash table implementation used in the Lua core. This makes sense because values of any Lua data type (other than nil) can be used as both keys and values; but only strings and numbers have any kind of sensible ordering, and then only between values of like type.
For example, what should the sorted order of this table be:
unsortable = {
answer=42,
true="Beauty",
[function() return 17 end] = function() return 42 end,
[math.pi] = "pi",
[ {} ] = {},
12, 11, 10, 9, 8
}
It has one string key, one boolean key, one function key, one non-integral key, one table key, and five integer keys. Should the function sort ahead of the string? How do you compare the string to a number? Where should the table sort? And what about userdata and thread values which don't happen to appear in this table?
By convention, values indexed by sequential integers beginning with 1 are commonly used as lists. Several functions and common idioms follow this convention, and table.sort is one example. Functions that operate over lists usually ignore any values stored at keys that are not part of the list. Again, table.sort is an example: it sorts only those elements that are stored at keys that are part of the list.
Another example is the # operator. For the above table, #unsortable is 5 because unsortable[5] ~= nil and unsortable[6] == nil. Notice that the value stored at the numeric index math.pi is not counted even though pi is between 3 and 4 because it is not an integer. Furthermore, none of the other non-integer keys are counted either. This means that a simple for loop can iterate over the entire list:
for i in 1,#unsortable do
print(i,unsortable[i])
end
Although that is often written as
for i,v in ipairs(unsortable) do
print(i,v)
end
In short, Lua tables are unordered collections of values, each indexed by a key; but there is a special convention for sequential integer keys beginning at 1.
Edit: For the special case of non-integral keys with a suitable partial ordering, there is a work-around involving a separate index table. The described content of tables keyed by string values is a suitable example for this trick.
First, collect the keys in a new table, in the form of a list. That is, make a table indexed by consecutive integers beginning at 1 with keys as values and sort that. Then, use that index to iterate over the original table in the desired order.
For example, here is foreachinorder(), which uses this technique to iterate over all values of a table, calling a function for each key/value pair, in an order determined by a comparison function.
function foreachinorder(t, f, cmp)
-- first extract a list of the keys from t
local keys = {}
for k,_ in pairs(t) do
keys[#keys+1] = k
end
-- sort the keys according to the function cmp. If cmp
-- is omitted, table.sort() defaults to the < operator
table.sort(keys,cmp)
-- finally, loop over the keys in sorted order, and operate
-- on elements of t
for _,k in ipairs(keys) do
f(k,t[k])
end
end
It constructs an index, sorts it with table.sort(), then loops over each element in the sorted index and calls the function f for each one. The function f is passed the key and value. The sort order is determined by an optional comparison function which is passed to table.sort. It is called with two elements to compare (the keys to the table t in this case) and must return true if the first is less than the second. If omitted, table.sort uses the built-in < operator.
For example, given the following table:
t1 = {
a = 1,
b = 2,
c = 3,
}
then foreachinorder(t1,print) prints:
a 1
b 2
c 3
and foreachinorder(t1,print,function(a,b) return a>b end) prints:
c 3
b 2
a 1
You can only sort tables with consecutive integer keys starting at 1, i.e., lists. If you have another table of key-value pairs, you can make a list of pairs and sort that:
function sortpairs(t, lt)
local u = { }
for k, v in pairs(t) do table.insert(u, { key = k, value = v }) end
table.sort(u, lt)
return u
end
Of course this is useful only if you provide a custom ordering (lt) which expects as arguments key/value pairs.
This issue is discussed at greater length in a related question about sorting Lua tables.
Because they don't have any order in the first place. It's like trying to sort a garbage bag full of bananas.

Resources