有没有办法让一个列表按两个字段排序,比如姓和名?
我知道.listOrderByLastAndFirst
和.list(sort:'last, first')
无效。
答案 0 :(得分:81)
Hates_标准答案对我来说似乎不起作用;将"last,first"
按顺序排列只会导致异常"Property 'last,first' not found"
。要在两个字段上订购,您可以执行以下操作:
def c = MyDomain.createCriteria()
def results = c.list {
and{
order('last','desc')
order('first','desc')
}
}
答案 1 :(得分:9)
这已经很老了,但帮助我找到了合适的解决方案。使用withCriteria快捷方式的“更干净”的代码示例:
def c = MyDomain.withCriteria {
and {
order('last', 'desc')
order('first', 'desc')
}
}
答案 2 :(得分:8)
此旧解决方案不再有效。请参阅下面的mattlary答案
您可能必须在HQL中编写自定义查找程序或使用Criteria Builder。
MyDomain.find("from Domain as d order by last,first desc")
或者
def c = MyDomain.createCriteria()
def results = c.list {
order("last,first", "desc")
}
答案 3 :(得分:5)
更复杂的订购标准,(在Grails 2.1.0中测试)
def c = MyDomain.withCriteria {
property {
order('last', 'desc')
}
order('first', 'desc')
}
首先通过MyDomain.property.last然后通过MyDomain.first
进行排序答案 4 :(得分:4)
此查询在第一个字段的基础上工作。当第一个字段为空时,它会被第二个字段缩短。
order('last','desc')
order('first','desc')
答案 5 :(得分:3)
我认为一个标准是最好的选择,但是你通过首先尝试查找器做了正确的事情。从GORM检索域对象时,尝试的正确顺序是:动态查找器,条件,HQL。
答案 6 :(得分:3)
你可以这样做
def results=MyDomain.findAll([sort:"last",order:'desc'],[sort:"first",order:'desc']);
这行代码将首先按照姓氏对域类“MyDomain”的结果进行排序,然后按人员的名字对结果进行排序。
答案 7 :(得分:2)
MyDomain.findAll(sort: ['first': 'desc','last':'desc'])
适用于grails-datastore-gorm:6.0.3
答案 8 :(得分:0)
如果要对其项目内容进行排序,则需要实现一个比较器,该比较器具有一些智能,可以根据多个属性决定排序顺序。
Groovy式比较器的一些示例显示为here
但是,如果您正在从数据库查询返回正在排序的列表,那么最好使用CrteriaQuery对其进行排序并对其进行排序
答案 9 :(得分:0)
我有同样的问题。 由于我的列表不是很大,因此我使用常规排序,因为我想对链接域的字段进行排序: CalendarData->景点
Private WithEvents ZVX_IF As VIF 'Public command_s As String
Private Sub CommandButton1_Click()
Const NUM_REPS As Long = 2 'works as expexted
Dim freq As Variant, i As Long 'the i works as expexted
Dim Buffer As String
Dim Index As Integer
Dim Trace, Stim As Variant
Dim dt As String
Set ZVX_IF = New VIF
Tabelle3.Range("B2").Value = "---"
'clear previous data
Range("A4:C4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
command_s = Space(20)
freq = 15.005005 'set as primary frequency
With ZVX_IF
If .OpenDevice(Tabelle3.Range("B1").Value) = True Then
.ReadString "*IDN?", Buffer: Tabelle3.Range("B2").Value = Buffer 'read out
instrument type
.WriteString ":FORMat:DATA REAL" 'set transfer to real
format (speed)
.ReadString ":INSTrument:PORT:COUNt?", Buffer: Tabelle3.Range("C2").Value =
Buffer ' detect no of ports
If Buffer = "2" Then
.WriteString command_y 'set measured quantity to Sxx
.WriteString ":CALCulate1:FORMat REAL" 'set format to real
.WriteString ":INITiate1:CONTinuous OFF" 'set single sweep
For i = 1 To NUM_REPS 'works as expexted
'ENTER YOUR HARMONIC
'Center the analyzer on a harmonic
.WriteString "FREQ:CENT " & freq & " MHz"
'Set Span to
.WriteString "FREQ:SPAN 912 kHz"
'now memorize current timer value
t = Timer()
'display current time
Tabelle3.Range("I16").Value = Now
'___________________________________
'start one sweep and wait for completion
.WriteString ":INITiate1:IMMediate;*WAI" 'launch single sweep
and wait to finish
'get stimulus data
.ReadFloatArray "TRAC:STIM? CH1DATA", Stim ' get stimulus data
'write stimulus values to sheet
For Index = LBound(Stim) To UBound(Stim)
Tabelle3.Range("A4").Offset(Index, 0).Value = Stim(Index) / 1000000000#
Next Index
'______________________________________
'now get trace data
.ReadFloatArray ":TRACe? CH1Data", Trace 'get all of channel 1
data
'show elapsed timer value on sheet
Tabelle3.Range("I44").Value = (Timer() - t) * 1000
'write data to worksheet
For Index = LBound(Trace) To UBound(Trace) Step 2
Tabelle3.Range("B4").Offset(Index / 2, 0).Value = Trace(Index)
Tabelle3.Range("C4").Offset(Index / 2, 0).Value = Trace(Index + 1)
Next Index
freq = freq + 10
'Save the workbook and Enter the Name you want it saved as
dt = Format(CStr(Now), "yyyy_mm_dd_hh_mm_ss")
ActiveWorkbook.SaveAs FileName:="C:\Users\Profesor Xavier\Desktop\ZHU\Data
run\_Test_loop_" & dt & ".xls", FileFormat:=56
Next i 'works as expexted
Else
Get_Spara16.Show '4-port instrument
End If
Else
Tabelle3.Range("B2").Value = "INSTRUMENT NOT FOUND"
End If
End With
End Sub