如何以特定的顺序返回数组?

时间:2021-11-21 04:14:02

On the client side as of now, it receives an array of record labels from the database. The code below is the query used with Sequelize to call all record labels.

到目前为止,在客户端,它接收来自数据库的一系列记录标签。下面的代码是与Sequelize一起用来调用所有记录标签的查询。

I do receive an array in order by names (strings), but

我确实通过名称(字符串)来接收数组,但是

the array is case sensitive, therefore if the name is capital is will be in the beginning of the array. I will give in an example of what it outputs:

数组是区分大小写的,因此如果名称是大写,则在数组的开头。我将给出它输出的一个例子:

currently:

目前:

A

一个

B

B

C

C

D

D

a

一个

b

b

c

c

d

d

but I'm trying to get it to return like this

但是我试着让它像这样返回。

A

一个

a

一个

B

B

b

b

C

C

c

c

D

D

d

d

If all else fails, I will just make some logic on the client side, but any suggestions would be appreciated.

如果一切都失败了,我将在客户端做一些逻辑,但任何建议都将是感激的。

router.get('/', function (req, res) {
  RecordLabel.findAll({
    order: ('name').toUpperCase()
  })
  .then(function (recordLabels) {
    return res.json(recordLabels)
  })
  .catch(function (err) {
    return res.json({ error: err})
  })
})

2 个解决方案

#1


1  

This order problem is not deriving from sequelize but postgresql. In some configurations posstgresql is not ordering even by primary key or id. If you have priviliages, changing database locales can work for you.

这个顺序问题不是源自sequelize,而是源自postgresql。在某些配置中,posstgresql甚至不是按主键或id排序的。

After a query has produced an output table (after the select list has been processed) it can optionally be sorted. If sorting is not chosen, the rows will be returned in an unspecified order. The actual order in that case will depend on the scan and join plan types and the order on disk, but it must not be relied on. A particular output ordering can only be guaranteed if the sort step is explicitly chosen.

查询生成输出表之后(在select列表被处理之后),可以选择排序。如果没有选择排序,将以未指定的顺序返回行。在这种情况下,实际的顺序将取决于扫描和连接计划类型以及磁盘上的顺序,但不能依赖它。只有显式地选择排序步骤,才能保证特定的输出顺序。

Look at this answer : link

看看这个答案:链接

#2


1  

如何以特定的顺序返回数组?use this :

用这个:

router.get('/', function (req, res) {
var sequelize=require('sequelize')
RecordLabel.findAll({
  order:[sequelize.fn('lower', sequelize.col('name'))]
})
.then(function (recordLabels) {
   return res.json(recordLabels)
})
.catch(function (err) {
  return res.json({ error: err})
  })
})

#1


1  

This order problem is not deriving from sequelize but postgresql. In some configurations posstgresql is not ordering even by primary key or id. If you have priviliages, changing database locales can work for you.

这个顺序问题不是源自sequelize,而是源自postgresql。在某些配置中,posstgresql甚至不是按主键或id排序的。

After a query has produced an output table (after the select list has been processed) it can optionally be sorted. If sorting is not chosen, the rows will be returned in an unspecified order. The actual order in that case will depend on the scan and join plan types and the order on disk, but it must not be relied on. A particular output ordering can only be guaranteed if the sort step is explicitly chosen.

查询生成输出表之后(在select列表被处理之后),可以选择排序。如果没有选择排序,将以未指定的顺序返回行。在这种情况下,实际的顺序将取决于扫描和连接计划类型以及磁盘上的顺序,但不能依赖它。只有显式地选择排序步骤,才能保证特定的输出顺序。

Look at this answer : link

看看这个答案:链接

#2


1  

如何以特定的顺序返回数组?use this :

用这个:

router.get('/', function (req, res) {
var sequelize=require('sequelize')
RecordLabel.findAll({
  order:[sequelize.fn('lower', sequelize.col('name'))]
})
.then(function (recordLabels) {
   return res.json(recordLabels)
})
.catch(function (err) {
  return res.json({ error: err})
  })
})