how to sort the json array by keys in descending order using angular - arrays

var arr = {
'2021-07-20-21:10': {
sends: 1,
recvs: 1,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 0,
appErrors: 0,
responseTimeAvg: 172,
responseTimeMax: 172,
when: '21:10',
hostCount: 1,
},
'2021-07-20-21:22': {
sends: 1,
recvs: 0,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 0,
responseTimeMax: 0,
when: '21:22',
hostCount: 1,
},
'2021-07-20-21:13': {
sends: 2,
recvs: 1,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 89,
responseTimeMax: 177,
when: '21:13',
hostCount: 2,
},
'2021-07-20-21:14': {
sends: 1,
recvs: 0,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 0,
responseTimeMax: 0,
when: '21:14',
hostCount: 1,
}}
and it has to be sorted based on keys in desc order
var arr = {
'2021-07-20-21:22': {
sends: 1,
recvs: 1,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 0,
appErrors: 0,
responseTimeAvg: 172,
responseTimeMax: 172,
when: '21:22',
hostCount: 1,
},
'2021-07-20-21:14': {
sends: 1,
recvs: 0,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 0,
responseTimeMax: 0,
when: '21:14',
hostCount: 1,
},
'2021-07-20-21:13': {
sends: 2,
recvs: 1,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 89,
responseTimeMax: 177,
when: '21:13',
hostCount: 2,
},
'2021-07-20-21:10': {
sends: 1,
recvs: 0,
notSents: 0,
rejects: 0,
xptSkips: 0,
timeouts: 1,
appErrors: 0,
responseTimeAvg: 0,
responseTimeMax: 0,
when: '21:10',
hostCount: 1,
}}

You can extract the "keys" of your array using the Object.keys() function, then use the sort function to sort that array of keys, and generate a new object iterating over each keys there... Code would be (keeping arr as the name of your array):
// Get the keys of your "array"
let arrayOfKeys = Object.keys(arr)
// We sort it out...
let sortedArray = arrayOfKeys.sort()
// We will create the "final" array
let resultingArray = {}
// Now we iterate over each element, in order:
sortedArray.forEach((element) => {
resultingArray[element] = arr[element]
})
...of course this can be shortened wildly, for example:
var resultingArr = {}
Object.keys(arr).sort().forEach(e -> { resultingArr[e] = arr[e] })
EDIT: I see that you need to sort the array in descending order. This can be done using a custom sort function, or using the function reverse(). Just correct the sorting step:
// We sort it out...
let sortedArray = arrayOfKeys.sort().reverse()

Related

How to change datatype of column in FeatureCollection in GEE

I have FeatureCollection with 4 columns with different datatypes. One of them is "long" which I neet to converte to "Int". The FeatureCollection was imported as shapefiles. I need this column as "Int" to do classification.
type: FeatureCollection
id: users/aleksandramonika/pola_beskidy_trening
version: 1667257388739078
columns: Object (4 properties)
id: Long
class: Long
name: String
system:index: String
I don't know how to access and change type of a column.
I tried to change the value with remap, but the datatype was changed to "any".
FeatureCollection (30 elements, 4 columns)
type: FeatureCollection
columns: Object (4 properties)
id: Long
class:
name: String
system:index: String
var agriculture = ee.FeatureCollection(agriculture).remap(
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'class');

Split 16x16 matrix in 4x4 in the same matrix

Hi guys I am doing a small exercise splitting a 16x16 matrix in 4x4 chunks, creating a sort of diagonal of small matrix. I am representing the 4x4 matrix with a value of 1 and the rest with a value of 0, painting them in the big matrix itself.
This what it should look like:
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
"0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1"
This is what I have achieved at me moment:
The red squares is what I am missing. These 4x4 matrix has to be "1" as well.
This is code (it is in javascript but I don't really care about the language).
let m = Array(16)
.fill("0")
.map(() => Array(16).fill("0"));
for (let i = 0; i < m.length; i++) {
for (let j = 0; j < m.length; j++) {
if (i % 8 < 4 && j % 8 < 4) {
m[i][j] = "1";
}
}
}
m.map((a) => console.log(JSON.stringify(a + "")));
Anyone know how to solve this problem? Thank You in advance!
I'd try something like that: it uses even/odd comparison to determine position for "1"
let m = Array(16)
.fill("")
.map(() => Array(16).fill(""));
for (let i = 0; i < m.length; i++) {
for (let j = 0; j < m.length; j++) {
let mi = parseInt(i/4);
let mj = parseInt(j/4);
m[i][j] = ((mi&1) == (mj&1))?"1":"0";
}
}
m.map((a) => console.log(JSON.stringify(a + "")));
In Ruby:
require 'matrix'
m = Matrix.build(16, 16) { |i,j| (i/4).even? == (j/4).even? ? 1 : 0 }
To convert the matrix object to an array:
m.to_a
#=> [[1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]]
See Matrix::build and Enumerable#to_a.
This should translate easily to other languages that have a matrix library, but even if such a library is not available, the clause
(i/4).even? == (j/4).even? ? 1 : 0
shows how the array can be constructed in an economic way.
Here's another way that does not use matrices.
a1 = [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0]
a2 = a1.reverse
#=> [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
Array.new(16) { |i| ((i/4).even? ? a1 : a2).dup }
See Array::new and Kernel#dup.

Finding distance between every point in a matrix

def create_zero_matrix(n,m):
return [[0 for i in range(m)] for j in range(n)]
def m_tight_print(m):
for i in range(len(m)):
line = ''
for j in range(len(m[0])):
line += str(m[i][j])
print(line)
def pd_map(r,c,sites):
blank = create_zero_matrix(r,c)
for count, site in enumerate(sites):
blank[site[0]][site[1]] = count #locating the shops
Hello, how do I calculate the distance of every point in my matrix from a specific point such as [1,3] (point 0) or [4,7] (point 1) or [7,2] (point 2) and change that particular value such that it shows the point that is closest to that particular coordinate?
>>> pprint(pizzaMap)
[[0, 0, 0, 0, 0, 0, 0, 'X', 1, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 'X', 1, 1, 1]
['X', 0, 0, 0, 0, 0, 1, 1, 1, 1]
[2, 2, 2, 2, 2, 'X', 1, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 1, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 2, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 2, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 2, 'X', 1, 1]]
This above is the test case but I can only get this from my code.
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 2, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
```.
I tried to use numpy.argwhere() but in the end I could not do it. What code must I use such that I am able to get my test case?

How to find the number of elements of a two-dimensional array, read from a file?

I have a file. Inside the file I have stored a two-dimensional array, something like this:
[[0, 0, 1, 0, 1, 0, 1, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 0], [0, 1, 1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Lengths of arrays can vary and they are not always 10 elements long.
I read the array from the file using this method:
map = IO.readlines("test.txt")
and when i print the result using:
map.each {|x| puts "#{x}"}
the output is what I expect it to be. But if I try to get the row length using:
puts map[0].length
I get 320 instead of 10 (which is what I expect).
Can someone explain me why am I getting 320 instead of 10 ?
Instead of IO#readlines you should use JSON#parse since it’s a valid json:
require 'json'
JSON.parse(File.read("test.txt"))
#⇒ [[0, 0, 1, 0, 1, 0, 1, 0, 1, 0],
# [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
# [0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
# [0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
# [0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
# [0, 1, 1, 0, 1, 0, 1, 0, 1, 0],
# [0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
# [0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

"Array index is less than one" error at Winbugs

I am working on a multilevel differential item functioning model using WinBUGS package. I have successfully built simpler models, but I have also gotten the error "array index is less than one". I'd be very pleased if you could help.
# Model
Model
{
for (l in 1:50){
y[l] ~ dbern(p[l])
logit(p[l])<- u2[stu[l]] - beta[x[l]] + gamma[tea[l], x[l]]*grp[l] + alpha1[x[l]]*geo[l] +
alpha2[x[l]]*conf[l] + alpha3[x[l]]*ses[l]
}
for (t in 1:10){
for (i in 1:10){
gamma[t,i] ~ dnorm(gamma.hat[t,i], tau.gamma[i])
gamma.hat[t,i]<-pi1[i] + pi2[i]*inq[t]
}
}
# fixed effect prior
for (i in 1:10){
beta[i] ~ dnorm(0, .0001)
alpha1[i] ~ dnorm(0, .0001)
alpha2[i] ~ dnorm(0, .0001)
alpha3[i] ~ dnorm(0, .0001)
pi1[i] ~ dnorm(0, .0001)
pi2[i] ~ dnorm(0, .0001)
}
# Random effect prior
for (s in 1:5){
u2[s] ~ dnorm(0,tau.u2)
}
tau.u2 <- pow(sigma.u2, -2)
sigma.u2 ~ dunif (0, 100)
for (i in 1:10){
tau.gamma[i] <- pow(sigma.gamma[i],-2)
sigma.gamma[i] ~ dunif(0,100)
}
}
# Data
list(y=c(0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), ses=c(0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1), conf=c(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1), geo=c(1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1), grp=c(1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim=c(10, 10)))
Indexes start from 1, not 0, in BUGS. The variables stu and tea are used for indexing, but they take the values 0,1 - they should be 1,2 instead.
Something going on with x. It is one shorter in length than the other variables, perhaps you are missing an observation.

Resources