# [922\. 按奇偶排序数组 II](https://leetcode-cn.com/problems/sort-array-by-parity-ii/)
![](https://box.kancloud.cn/4ff213db7cb98422269b976ccb1cad8d_691x403.png)
```
func sortArrayByParityII(A []int) []int {
var a []int // 奇数
var b []int // 偶数
for _,v := range A {
if v % 2 == 0 {
b = append(b,v)
}else if v != 0{
a = append(a,v)
}
}
for k,_ := range A {
switch {
case k == 0:
A[k] = b[0]
b = append(b[1:])
case k % 2 == 0:
A[k] = b[0]
b = append(b[1:])
case k % 2 != 0:
A[k] = a[0]
a = append(a[1:])
}
}
return A
}
```
方法二:
~~~
// 方法二
func TestDemo922Pro(t *testing.T) {
data := []int{4, 2, 5, 7, 8, 9}
for i := 0; i < len(data); i++ {
if i == 0 || i%2 == 0 {
if data[i]%2 != 0 {
for j := len(data) - 1; j > i; j-- {
if data[j]%2 == 0 {
data[i], data[j] = data[j], data[i]
}
}
}
} else {
if data[i]%2 == 0 {
for j := len(data) - 1; j > i; j-- {
if data[j]%2 != 0 {
data[i], data[j] = data[j], data[i]
}
}
}
}
}
fmt.Println(data)
}
~~~